Android: Receive Sms Even If App Is Closed
Solution 1:
You can have the receiver run as a Service. Here is the code I use:
public class Communicator extends Service
{
private final String TAG = this.getClass().getSimpleName();
private SMSReceiver mSMSreceiver;
private IntentFilter mIntentFilter;
@Override
public void onCreate()
{
super.onCreate();
Log.i(TAG, "Communicator started");
//SMS event receiver
mSMSreceiver = new SMSReceiver();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
mIntentFilter.setPriority(2147483647);
registerReceiver(mSMSreceiver, mIntentFilter);
Intent intent = new Intent("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);
}
}
@Override
public void onDestroy()
{
super.onDestroy();
// Unregister the SMS receiver
unregisterReceiver(mSMSreceiver);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
private class SMSReceiver extends BroadcastReceiver
{
private final String TAG = this.getClass().getSimpleName();
@Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
String strMessage = "";
if ( extras != null )
{
Object[] smsextras = (Object[]) extras.get( "pdus" );
for ( int i = 0; i < smsextras.length; i++ )
{
SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]);
String strMsgBody = smsmsg.getMessageBody().toString();
String strMsgSrc = smsmsg.getOriginatingAddress();
strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;
if (strMsgBody.contains(Constants.DELIMITER)) {
Intent msgIntent = new Intent(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:
<service android:name="yourpakage.Communicator" />
Now you can start listening for incomming SMS messages by starting the service:
startService(new Intent(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(new Intent(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"