0

How can I send data from a Service to an Activity? Broadcast receiver? Handlers? Intent? I have several Strings in particular that I would like to send from Service to an Activity, so that I can then display some View to the user.

getApplication().startActivity(new Intent()) ?

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
user817129
  • 522
  • 3
  • 10
  • 19
  • I did some more reading and I'm thinking about using Handler. If I define my custom Handler in the Receiving class (to get the data), then I can just send messages from the Service class, right? – user817129 Jul 08 '11 at 11:02

3 Answers3

0

Since both Activity & Service is in same application. Instead of going for Service Binder IPC, try implementing register callBack mechanism. Try follow below step. -> Make a static class which extends from class Application to make quick reference anywhere in your application. -> Register a callBack from Activity to this application class. -> Post event from your service to the application class and let Application class deliver the event to all register callBacks.

Sukumar
  • 1,303
  • 10
  • 15
0

As @justin-shield mentioned the best way is to use some form of IPC. I don't think you need to use AIDL, however. You can see another answer I gave to a similar question that outlines the basic steps to register Handlers and Messengers in your Activity and your Service.

Community
  • 1
  • 1
zeitkunst
  • 505
  • 5
  • 11
0

I think all your answers are way more complex than what I need. I think I have come up with a rather simple solution (psuedo-code):

Service:

Intent i = new Intent(this,Activity.class);

i.putExtra(name,value);

startActivity(i);

Activty:

private Intent intent;

private String s1;

from the

onCreate(Bundle savedInstanceState)
{
    super(savedInstanceState);

intent = this.getIntent(); //Gets the intent that started this Activity.    

s1=intent.getExtras().getString(name);      

}

Would I use same code in onStart() ? What about onResume() ? onRestart() ? Lastly, I only want to send to the Activity some data to publish via some View objects, I don't need any communication going back to the Service.

user817129
  • 522
  • 3
  • 10
  • 19