5

I can find several examples of how to build a service, but I'm having a difficult time finding a working example of how to send messages between an Activity and a Service. From what I can find, I think my options are to use Intents, AIDL, or to use the service object itself as per this question.

In my case, my activity is the only activity that will ever access the service, so a local service will do. When the activity is open, I want to see some status messages from the service, which will be coming in at up to 20 Hz. Are there any limitations on how many messages per second those communications methods will support? Basically, which method is going to be best for my situation?

Thanks.

Community
  • 1
  • 1
Lance Lefebure
  • 6,845
  • 6
  • 24
  • 18

2 Answers2

5

Since your Actvity and Service are a part of the same app, then no need to use AIDL. You may simply use your Service as a local one.

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
  • The Local Service Example on that page does a great job of explaining how to create a service, but it doesn't show how to communicate between the service and activity. Can I use the Messenger class like the Remote Messenger Service Sample shows, while in a local service? – Lance Lefebure Nov 28 '10 at 17:06
1

The limitation is only affected by the performance of your device. There is no cap on requests per second. Usually there is a context switch involved, that uses quite a lot of cpu (compared to other parts of the transmission), but since you use a local service you don't suffer from that. In any case, 20Hz is not a problem. The best solution for you would be to use AIDL, and set up a callback that the service can call to report its status.

There is good example of how this is done in the APIDemos.

Robert
  • 6,855
  • 4
  • 35
  • 43