5

我正在尝试将自定义 ResultReciever 从意图使用到 intentService,但我收到了这个奇怪的错误。

任何想法为什么?

按照本指南使用 resulReciever 作为回调

http://lalit3686.blogspot.co.uk/2012/06/how-to-update-activity-from-service.html

这是我启动服务的活动的代码:

private void doNetworkInitCalls() {
    intent = new Intent(getApplicationContext(), NetworkService.class);
    intent.setAction(NetworkService.ACTION_GET_CITY_INFO);
    intent.putExtra(NetworkService.EXTRA_LATLON, new LatLon(51.5073, 0.1276));
    MyReciever reciever = new MyReciever(null);
    reciever.setOnCityInfoRecieved(this);
    intent.putExtra(MyReceiver.RESULT_RECEIEVER_EXTRA, reciever);
    startService(getCityInfoIntent);
}

MyReciever 类:

public class MyReciever extends ResultReceiver {

    public static final String RESULT_CITY_INFO = "cityInfoData";
    public static final String RESULT_RECEIEVER_EXTRA = "reciever";

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

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        super.onReceiveResult(resultCode, resultData);
    }
}

意向服务:

public class NetworkService extends IntentService {

 @Override
 protected void onHandleIntent(Intent intent) {
     if (intent != null) {
         MyReciever reciever = intent.getParcelableExtra(MyReciever.RESULT_RECEIEVER_EXTRA); //fails on this line
    }
}
4

1 回答 1

13

那这个呢 :

public class NetworkService extends IntentService {

 @Override
 protected void onHandleIntent(Intent intent) {
     if (intent != null) {
         ResultReceiver reciever = intent.getParcelableExtra(MyReciever.RESULT_RECEIEVER_EXTRA);
    }
}

由于 CREATOR 尚未在 MyReciever 中重新定义,因此它会创建 ResultReceiver 实例。只需从 Intent 中接收您的 ResultReceiver 实例,即 ResultReceiver。

于 2014-10-15T14:08:58.713 回答