什么是从IntentService处理回调的最佳方式 [英] What is the best way to handle callback from IntentService

查看:819
本文介绍了什么是从IntentService处理回调的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从我的Andr​​oid应用程序web服务的调用。
在调用和结果的IntentService内处理。
现在,我使用一个类(MyReceiver)从ResultReceiver延伸。

I am currently working on webservice calls from my Android application. The calls and results are handled within an IntentService. For now, I'm using a class (MyReceiver) which extends from ResultReceiver.

public class ResultReceiverWS extends ResultReceiver {
    private Receiver mReceiver;

    public ResultReceiverWS(Handler handler) {
        super(handler);
    }

    public void setReceiver(Receiver receiver) {
        mReceiver = receiver;
    }

    public interface Receiver {
        public void onReceiveResult(int resultCode, Bundle resultData);
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        if (mReceiver != null) {
            mReceiver.onReceiveResult(resultCode, resultData);
        }
    }
}

我从实现MyReceiver.Receiver的类的实例捆绑像上面code中的活动传递:

I pass from the Activity which implements MyReceiver.Receiver the instance of the class in a bundle like above code :

MyReceiver receiver = new MyReceiver (new Handler());
receiver.setReceiver(this);
Intent call = new Intent(getApplicationContext(), ClientWS.class);
call.putExtra(RECEIVER, receiver);
call.putExtra(ACTION, MYMETHOD);
startService(call);

问题是,接收器连接到活动的线程,如果活动被杀害,重新创建(通过旋转屏幕或其他)在无人区,接收点......

The problem is that receiver is attached to the Activity thread, if activity is killed, re-created (by turning screen or whatever), receiver points in no man's land ...

所以,我的问题是如何处理这种情况。我鑫卡特约广播接收器,但我不知道这一点。

So, my question is how to handle this cases. I've thinked about BroadcastReceiver but I'm not sure about that.

任何建议,欢迎在这里。

Any suggestions are welcome here.

推荐答案

使用 LocalBroadcastManager

或者使用第三方事件总线,如奥托或的 EventBus

Or, use a third-party event bus, like Otto or EventBus.

或者,如果该Web服务调用的结果将只通过请求它的活动中使用,切换到的AsyncTask 由保留的片段,其中管理情况下,你可以与UI更自然的工作。

Or, if the result of this Web service call will only ever be used by the activity that requested it, switch to an AsyncTask managed by a retained fragment, in which case you can work with the UI more naturally.

或者,如果你需要让用户了解的结果,即使你的UI是不是在前台,使用的有序广播方式

Or, if you need to let the user know about the results even if your UI is not in the foreground, use the ordered broadcast pattern.

这篇关于什么是从IntentService处理回调的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆