Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can i INTERCEPT an Incoming SMS With A specific Text

Tags:

android

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.

like image 291
Max Avatar asked Aug 08 '11 08:08

Max


1 Answers

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")) {                      abortBroadcast();                  }     } 

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>     </intent-filter>  </receiver> 

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

like image 55
Christopher Gwilliams Avatar answered Sep 28 '22 08:09

Christopher Gwilliams