我正在尝试将自定义 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
}
}