43

I'm trying to start a Service from my Activity to look out for changes on a web page, it's a private app so I don't bother the battery life...

But I'd like to pass data from my Service to my Activity... I can't seem to find a way to call the Activity from my Service. How can I achieve this?

trejder
  • 17,148
  • 27
  • 124
  • 216
Mars
  • 4,197
  • 11
  • 39
  • 63

4 Answers4

48

As Alex indicated, you can bind to the service and pass some sort of listener or callback to the service to use on events.

Or, you can use a broadcast Intent, perhaps using methods like setPackage() on the Intent to limit the scope of the broadcast.

Or, you can use createPendingResult() to create a PendingIntent that you pass as an Intent extra to the service -- the service can then use that PendingIntent to trigger onActivityResult() in your activity.

Or, you can use a ResultReceiver.

Or, you can use a Messenger.

(admittedly, I have not tried those latter two approaches, but I think they will work here)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    I'm trying to use the pending intent to call onactivityresult, but it isn't being called at all – Mars Nov 06 '10 at 13:36
  • Intent notificationIntent = new Intent(this, activity.class); notificationIntent.putExtra("captchaUrl", captchaUrl); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); – Mars Nov 06 '10 at 13:37
  • @mars: What you are doing is not what I wrote. I wrote: "Or, you can use `createPendingResult()` to create a `PendingIntent` that you pass as an `Intent` extra to the service -- the service can then use that `PendingIntent` to trigger `onActivityResult()` in your activity." I have not attempted to use `createPendingResult()` with a `Notification`, but I suspect that this will not work. Bear in mind that you wrote originally, "I'd like to pass data from my service to my activity", which has nothing to do with a `Notification`. – CommonsWare Nov 06 '10 at 13:52
  • you're right but let's say I wanted to use an intent to send data to my activity how would I do that? what event is call by my activity when it gets an intent? – Mars Nov 06 '10 at 17:55
  • @mars: "you're right but let's say I wanted to use an intent to send data to my activity how would I do that?" -- if you want to change the scenario from your original StackOverflow question, please ask a new StackOverflow question, and adequately explain your scenario, beyond "wanted to use an intent to send data to my activity". – CommonsWare Nov 06 '10 at 17:58
  • How do you pass the PendingIntent? putExtra doesn't complain about passing a variable of type PendingIntent as the second parameter, but I don't know how to retrieve it on the other end. – Michael May 21 '12 at 18:45
  • @Michael: Call `getParcelableExtra()`. – CommonsWare May 21 '12 at 21:52
3

One more alternative: if your service updates content provider, activity can be notified via ContentObserver. This would be enough if your service downloads some data from server and you simply want to display fresh contents in the activity.

Juozas Kontvainis
  • 9,461
  • 6
  • 55
  • 66
1

Some ugly ways:

1.) If the activity has not started yet, then use intent and startActivity, but remember intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

2.) Otherwise if the activity has started already, you can write your own callback method in the activity and register the method in the service, then direct call the method in the service.

Hope to find some smart way.

I think broadcast also work well, you can write a static inner class for receive broadcast and start activity. But it is also ugly in my opinion.

joran
  • 169,992
  • 32
  • 429
  • 468
vvsueprman
  • 143
  • 1
  • 2
  • 10
0

The ResultReceiver mechanism has been explained in another post :- Restful API service However it would not work in all cases. Please refer to my comment on that post. The limited scope broadcast or PendingIntent mechanism seem more suitable.

Community
  • 1
  • 1
Nikhil_Katre
  • 555
  • 6
  • 10