0

I have a few applications that connect to a network service in order to call a few messages. As the communication with this network service is planned to change, I was thinking to implement a service that will deal with the communication with the network service, and the applications will just send intents or messages to this service. My idea is to have this service standalone, started after boot up, so it needs to be started service. It will connect to the network service and will wait for commands from the applications. So far, I found that Messenger is nearest to what I need, but I think it can only work as a bound service.

Is there already something I can use for my case? If not, then am I correct that I would need to:

  • implement a started service
  • use HandlerThread to handle the input messages - my preferred way is to use intents, but so far I don't know how to do it
4

1 回答 1

0

So as pskink pointed out, the way to go is using Messenger. I used com.example.android.apis.app.MessengerService (from Android SDK samples) as an example of such service. The service is started on reception of ACTION_BOOT_COMPLETED and (on my device with Android 4.2) when the first client tries to bind to the service, the service's onCreate and onBind are called. The communication occurs by sending Message objects and I have defined an enum with the supported messages listed (so I am not using AIDL directly).

于 2014-07-31T14:09:29.197 回答