Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassCastException: android.os.ResultReceiver cannot be cast to com.hello.utils.network.MyReciever

i am trying to use a custom ResultReciever from an intent into a intentService but i get this bizare error.

any ideas why?

Followed this guide in using resulReciever as callbacks

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

Here is my code from activity that starts service:

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 class:

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);
    }
}

IntentService:

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
    }
}
like image 666
Jonathan Avatar asked Oct 15 '14 13:10

Jonathan


1 Answers

What about this :

public class NetworkService extends IntentService {

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

As CREATOR has not been redefined in MyReciever, it creates ResultReceiver instances. Just receive your ResultReceiver instance from the intent as it is, a ResultReceiver.

like image 169
ToYonos Avatar answered Nov 07 '22 12:11

ToYonos