Here is a 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 is broadcast receiver 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 intent from broadcast receiver to my running service. thanks in advance.
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 anIntent
toonStartCommand()
of the service, whether it is already running or not. Ideally, your service is anIntentService
, 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 myWakefulIntentService
, which wraps up theIntentService
+WakeLock
pattern.