Max Max - 1 year ago 77
Android Question

How Can i INTERCEPT an Incoming SMS With A specific Text

I want to know about intercepting an incoming sms for a specific key word EX:"Hi", so that I can read that sms containing "Hi" in it & delete it after reading the msg, & if that msg doesn't contain any such text then it wouldn't be deleted and instead saved in the inbox. Guys please help me I am finding it very difficult to perform this functionality.

Answer Source

Look for Broadcast Receiver, this is dependent on the apps installed on the phone but you can give your app priority for listening to messages. Although, when a notification is shown, the message won't be in the SMS Database yet, so you will need to use abortBroadcast() to stop other apps being notified. See example below:

public class MessageReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
             Bundle pudsBundle = intent.getExtras();
             Object[] pdus = (Object[]) pudsBundle.get("pdus");
             SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);    
             Log.i(TAG,  messages.getMessageBody());
                 if(messages.getMessageBody().contains("Hi")) {

And you would need to declare the receiver in the manifest, like so:

 <receiver android:name="com.encima.smsreceiver.MessageReceiver" android:exported="true">
    <intent-filter android:priority="999">
        <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>

Finally, make sure you have the permission to RECEIVE_SMS in the manifest, hope that helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download