Skip to content Skip to sidebar Skip to footer

Android: Receive Sms Even If App Is Closed

I am writing an app, which should work even after the app is closed. Here is the code which I wrote to accept the incoming SMS. public class SMSReceiver extends BroadcastReceiver

Solution 1:

You can have the receiver run as a Service. Here is the code I use:

publicclassCommunicatorextendsService
{

privatefinalStringTAG=this.getClass().getSimpleName();

private SMSReceiver mSMSreceiver;
private IntentFilter mIntentFilter;

@OverridepublicvoidonCreate()
{
    super.onCreate();


    Log.i(TAG, "Communicator started");
    //SMS event receiver
    mSMSreceiver = newSMSReceiver();
    mIntentFilter = newIntentFilter();
    mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
    mIntentFilter.setPriority(2147483647);
    registerReceiver(mSMSreceiver, mIntentFilter);

    Intentintent=newIntent("android.provider.Telephony.SMS_RECEIVED");
    List<ResolveInfo> infos = getPackageManager().queryBroadcastReceivers(intent, 0);
    for (ResolveInfo info : infos) {
        Log.i(TAG, "Receiver name:" + info.activityInfo.name + "; priority=" + info.priority);
    }
}

@OverridepublicvoidonDestroy()
{
    super.onDestroy();

    // Unregister the SMS receiver
    unregisterReceiver(mSMSreceiver);
}

@Overridepublic IBinder onBind(Intent arg0) {
    returnnull;
}

privateclassSMSReceiverextendsBroadcastReceiver
{
    privatefinalStringTAG=this.getClass().getSimpleName();

    @OverridepublicvoidonReceive(Context context, Intent intent)
    {
        Bundleextras= intent.getExtras();


        StringstrMessage="";

        if ( extras != null )
        {
            Object[] smsextras = (Object[]) extras.get( "pdus" );

            for ( inti=0; i < smsextras.length; i++ )
            {
                SmsMessagesmsmsg= SmsMessage.createFromPdu((byte[])smsextras[i]);

                StringstrMsgBody= smsmsg.getMessageBody().toString();
                StringstrMsgSrc= smsmsg.getOriginatingAddress();

                strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;                    


                if (strMsgBody.contains(Constants.DELIMITER)) {

                    IntentmsgIntent=newIntent(Constants.INTENT_INCOMMING_SMS);
                    msgIntent.putExtra(Constants.EXTRA_MESSAGE, strMsgBody);
                    msgIntent.putExtra(Constants.EXTRA_SENDER, strMsgSrc);
                    sendBroadcast(msgIntent);

                    this.abortBroadcast();
                }
            }



        }

    }

}
}

Remember to add it to the manifest:

<serviceandroid:name="yourpakage.Communicator" />

Now you can start listening for incomming SMS messages by starting the service:

startService(newIntent(this, Communicator.class));

I stop the service in onDestroy of my Activity but I suppose you can keep it running and even register it to be started when the device is booted as with any other service.

Here is the code to stop the service:

stopService(newIntent(this, Communicator.class));

In case you also want to send SMS at some point I have made a rather extensive answer on the subject here: Send SMS until it is successful

Solution 2:

receiver should be in manifest; also keep in mind that you don't have a guarantee to receive SMS even with declared receiver's highest priority - other installed apps which happened to receive SMS Intent from OS may cancel further Intent propagation

Solution 3:

If you registering you receiver in activity you must unregister it! It's very important to avoid memory leaks.

If you want to receive sms at any time you must registet it in AndroidManifest file

Post a Comment for "Android: Receive Sms Even If App Is Closed"