You should look at creating your own ResultReceiver
subclass in your activity. ResultReceiver
implements Parcelable
so can be passed from your Activity
to your Service
as an extra on the Intent
.
You’ll need to do something like this:
-
Implement a subclass of
ResultReceiver
within your activity class. The key method to implement isonReceiveResult()
. This method provides you a withBundle
of result data which can be used to pass whatever information you are retrieving in yourService
. Simply unpack the data you need and use it to update your activity. -
In your activity, create a new instance of your custom
ResultReceiver
and add it to theIntent
you use to start your service. -
In your
Service
‘sonStartCommand()
method, retrieve theResultReceiver
passed in on theIntent
and store it in a local member variable. -
Once your
Service
completes its work, have it callsend()
on the ResultReceiver passing whatever data you want to send back to the activity in aBundle
.
This is a pretty effective pattern and means you’re not storing data in nasty static variables.