19

How can I code in Android such that my app can analyze an incoming SMS and perhaps block it or do something(maybe move to a different SMS folder) BEFORE the SMS actually raises a notification telling the user of a new SMS? I would target Android 2.1 and above.

I would want to analyse incoming SMS for user specified spam words, and if found would want to delete/mark as read/move the message to a different folder.

Saurabh Kumar
  • 2,329
  • 6
  • 32
  • 52

3 Answers3

24

I use this code, as a BroadcastReceiver:

public void onReceive(Context context, Intent intent) 
{   
    //this stops notifications to others
    this.abortBroadcast();

    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();   
    SmsMessage[] msgs = null;
    String str = "";            
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            str += "SMS from " + msgs[i].getOriginatingAddress();
            from = msgs[i].getOriginatingAddress();
            str += " :";
            str += msgs[i].getMessageBody().toString();
            msg = msgs[i].getMessageBody().toString();
            str += "\n"; 
        }
        if(checksomething){
            //make your actions
            //and no alert notification and sms not in inbox
        }
        else{
            //continue the normal process of sms and will get alert and reaches inbox
            this.clearAbortBroadcast();
        }
  }

remember to add it in manifest and add a higgest priority (100) for broadcast or sms will go first to inbox and get the alert notification.

    <receiver android:name=".SmsReceiver">
        <intent-filter android:priority="100">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter>
    </receiver>

Hope it helps you.

maztch
  • 1,597
  • 19
  • 25
0

Well u can trap the incoming SMS, but i think u will not be able to block the notification .....
If u want to delete SMS here is a thread that can help....
How to delete an SMS from the inbox in Android programmatically?

Community
  • 1
  • 1
viv
  • 6,158
  • 6
  • 39
  • 54
  • well I need Android to NOT show the default 'new message' notification if my application has already handled the incoming SMS. So that is an important requirement for the best user experience :( – Saurabh Kumar Jan 09 '11 at 08:17
  • well there are many threads on StackOverflow that will tell u it's not possible without using your own extended classes of those that are hidden....... but yes you wan wait for other's reply also........ – viv Jan 09 '11 at 15:17
  • 1
    Now abortBroadcast(); method can be used for restricting the incoming message to go to inbox. – Arun Badole Jul 03 '12 at 12:19
0

This code works on my 2.3.3 device. HTC MyTouch 4g Slide. the abortBroadcast suppresses the notificationsound + notification on status bar + does not allow the SMS to go to the inbox. Some users had mentioned that it does not work on real devices and works only on emulator, that is not always the case. If the priority is 100 then on this particular device the code works as expected.

user
  • 86,916
  • 18
  • 197
  • 190