You need to create your own resultreceiver class, extended from ResultReceiver
then implements the resultreceiver interface in your activity
Pass the custom resultreceiver object for the intent of the Service and in the intenservice simply retrieves the recipient object and calls the receiver.send () function to send something that triggers the activity to the Bundle object.
here's the custom class of ResultReceiver:
public class MyResultReceiver extends ResultReceiver { private Receiver mReceiver; public MyResultReceiver(Handler handler) { super(handler);
implements the Myresultreceiver.receiver interface in your activity, creates a class variable
Public MyResultReceiver mReceiver;
initialize this variable in onCreate:
mReceiver = new MyResultReceiver(new Handler()); mReceiver.setReceiver(this);
Pass this mReceiver to the intent service through:
intent.putExtra("receiverTag", mReceiver);
and select in the IntentService, for example:
ResultReceiver rec = intent.getParcelableExtra("receiverTag");
and send something into action using rec as:
Bundle b=new Bundle(); rec.send(0, b);
this will be received in onReceiveResult activity. You can view the full code at: IntentService: return data to activity
Edit: you must call setReceiver (this) in onResume and setReceiver (null) in onPause () to avoid leaks.
SohailAziz Apr 22 '12 at 10:00 2012-04-22 10:00
source share