Sending intent from broadcast receiver to running service in android

Here is my broadcast receiver.

public class SmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context ctx, Intent intent) { Log.d(DEBUG_TAG, SmsReceiver.class.getSimpleName()+ " action: " + intent.getAction()); // here is codes for sending intent to my running service } } 

Here are the broadcast receive and service nodes in my manifest XML file.

 <service android:name=".SMSGatewayService" /> <receiver android:name=".SmsReceiver"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> 

How can I send an intent from a broadcast receiver to my current service. thanks in advance.

+2
source share
1 answer

Ideally, you do not have a working service. Services should only be in memory when they do something, and the likelihood that your code will not do anything when SMS arrives.

In this case, your recipient can call startService() . It passes the Intent to the onStartCommand() service, regardless of whether it is running or not. Ideally, your service is an IntentService , one designed to work with a command template and to shut down when processing a command.

In addition, since SMS messages can appear while the device is sleeping, you will probably need to use WakeLock to ensure sufficient device downtime for the control to be transferred to the service and for the service to do whatever it is. One approach for this is to use the WakefulIntentService , which completes the IntentService + WakeLock .

+10
source

All Articles