我目前正在处理来自我的 Android 应用程序的 Web 服务调用。调用和结果在 IntentService 中处理。目前,我正在使用从 ResultReceiver 扩展的类 (MyReceiver)。
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 的 Activity 传递类的实例,就像上面的代码一样:
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);
问题是接收器连接到活动线程,如果活动被杀死,重新创建(通过转动屏幕或其他方式),接收器指向无人区......
所以,我的问题是如何处理这种情况。我考虑过 BroadcastReceiver 但我不确定。
欢迎在这里提出任何建议。