tags:

views:

21

answers:

2

Seems that a Service on Android is not the most popular method for implementing a persistent process for performing some background activity in Android. The suggestion is to use something like the alarm manager - however I am not sure if it is applicable to my situation. Advice welcomed.

What I am trying to implement is some background process that receives selected broadcasts. This process does some processing on the broadcast, and for example logs the results to a file. I can work out how to do this with a Service OK, but in earlier Android versions, the service seems to be killed relatively easily.

Are there alternatives to a service in this kind of application ??

Thanks

A: 

I think I have just found the answer... and basically the broadcast reciever is the mechanism for achieving it... The broadcast should be able to find the relevant receiver and start it for a limited period of time...

Need to check if i can receive cell location updates as a Broadcast Receiver though...

thanks

rwm
A: 

You can do it in your onReceive() method of the class that extends BroadcastReceiver.

Macarse
So long as the work is pretty short. Otherwise, have `onReceive()` call `startService()` on an `IntentService` that does the real work. `IntentService` will shut down when there is no more work to be done.
CommonsWare
The need for the IntentService is not clear to me... it seems that the IntentService spawns a separate thread to handle the processing - which may not actually be needed.
rwm
The need for using IntentService is not clear to me. IntentService spawns a new thread to run your application, but does not actually use the Services thread, except for starting your thread. How about an alternative of the onReceive() calling the startService on a normal Service. Then in the onStart() method you send a message to a Handler in your Service. This allows your onReceive() to return promptly, and then allows your Service thread to pick up the action and perform whatever activity you were planning to do ?
rwm
Is there a good example out there of using an IntentService from a broadcastreceiver ?
rwm