Fundamentally, I would like to establish a callback to an Activity from an IntentService. My question is very similar to the one answered here:
However, in the answer code, the activity code is seen as implementing a ResultReceiver. Unless I'm missing something, ResultReceiver is actually a class, so it cannot perform this implementation.
So essentially, I'm asking what would be the correct way to wire up a ResultReceiver to that service. I get confused with Handler and ResultReceiver concepts with respect to this. Any working sample code would be appreciated.
You need to make custom resultreceiver class extended from ResultReceiver
then implements the resultreceiver interface in your activity
Pass custom resultreceiver object to intentService and in intentservice just fetch the receiver object and call receiver.send() function to send anything to the calling activity in Bundle object.
here is customResultReceiver class :
implements the Myresultreceiver.receiver interface in you activity, create a class variable
initialize this variable in onCreate:
Pass this mReceiver to the intentService via:
and fetch in IntentService like:
and send anything to activity using rec as:
this will be received in onReceiveResult of the activity. You can view complete code at:IntentService: Providing data back to Activity
Edit: You should call setReceiver(this) in onResume and setReceiver(null) in onPause() to avoid leaks.
I have created a simple example that demonstrates how to use
ResultReceiver
.MainActivity:
MyService:
for use Resulteceiver in android
Create SomeResultReceiver extends from resultReceiver
Create interface someReceiver with on method for example onReceivResult(int resultCode,Bundle resultData);
3.use someReceiver in someResultreceiver
create someService extends IntentService and use someresultReceiver.send() method for send result from service to someOne class (ex: MyActivity)
Implement somereceiver on Activity
6.instantiation someResultReceiver in MyActivity class and setreceiver
for more details ResultReceiver Class see enter link description here
I would do it with EventBus nowadays
https://github.com/google/guava/wiki/EventBusExplained
You override a method by subclassing. It doesn't have to be an interface to do that.
For example: