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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With