Sending an intent from broadcast receiver to a running service in android

后端 未结 1 513
醉酒成梦
醉酒成梦 2021-01-07 06:34

Here is a my broadcast receiver.

public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context ctx, Intent intent) {
          


        
1条回答
  •  再見小時候
    2021-01-07 07:17

    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.

    0 讨论(0)
提交回复
热议问题