Here is a my broadcast receiver.
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
Ideally, you don't have a running service. Services should only be in memory when they are doing something, and odds are your code won't be doing anything when an SMS arrives.
That being said, your receiver can call startService()
. This passes an Intent
to onStartCommand()
of the service, whether it is already running or not. Ideally, your service is an IntentService
, one designed to work with the command pattern and to shut down when it is done processing a command.
Also, since SMS messages can arrive while the device is asleep, you will probably need to employ a WakeLock
to ensure the device stays awake long enough for control to pass to the service and for the service to do whatever it is it is doing. One approach for this is to use my WakefulIntentService, which wraps up the IntentService
+WakeLock
pattern.