Can we delete an SMS in Android before it reaches the inbox?

AndroidSms

Android Problem Overview


I am deleting an SMS from the inbox but I want to know: How can I delete it before it reaches the inbox?

Android Solutions


Solution 1 - Android

Yes.

Despite some negative reactions to this question, there are legitimate uses for SMS interception. For example: automating phone number verification, services which are provisioned via SMS (though generally this should be done with data SMS), or for applications which otherwise improve the user experience by processing specially-formatted messages in order to show them in a nice Android-specific UI.

As of Android 1.6, incoming SMS message broadcasts (android.provider.Telephony.SMS_RECEIVED) are delivered as an "ordered broadcast" — meaning that you can tell the system which components should receive the broadcast first.

If you define an android:priority attribute on your SMS-listening <intent-filter>, you will then receive the notification before the native SMS application.

At this point, you can cancel the broadcast, preventing it from being propagated to other apps.


Update (October 2013): When Android 4.4 arrives, it will make changes to the SMS APIs which may affect an app's ability to influence SMS delivery.
Check out this Android Developers blog post for some more info:
http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html

Solution 2 - Android

Step-1: Create your custom broadcast receiver to receive sms. write the logic to abort the broadst so that the message will not be available to your inbox

public class SMSReceiver extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
     if(conditionMatches){
     abortBroadcast();
     }
    }
}

Step-2 Register broadcast receiver in AndoridManifest and put android:priority value a large number

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

Thats It

How does the above code work

As SMS receiving broad cast is an ordered broadcast the receiver with high priority will receive first so your application receive first and after receiving you are aborting broadcast. So no other application can receive it. Hence the sms will not exist in inbox

Solution 3 - Android

The below("android:priority" and abortBroadcast()) solution works as long as Android Messaging application as default(I meant stock Android Messaging application). If user installs "GoSMSPro" or "HandcentSMS", these applications still show messages in inbox, I believe this due to "android:priority". I don't see any better way to fix the above issue, if third party messaging applications installed on the phone.

Solution 4 - Android

/**
 * Check priority
 * @param activity
 */
public static void receiverPriority(Activity activity){
	
	Intent smsRecvIntent = new Intent("android.provider.Telephony.SMS_RECEIVED");
	List<ResolveInfo> infos =  activity.getPackageManager().queryBroadcastReceivers(smsRecvIntent, 0);
	for (ResolveInfo info : infos) {
	    System.out.println("Receiver: " + info.activityInfo.name + ", priority=" + info.priority);
	}
}

Check priority and set higher priority (in your manifest) than other receivers.

Solution 5 - Android

If you have a scenario like this and you want to delete or ignore the message related to this contact number "+44xxxxx" etc, then use this code in SMS Broadcast receiver

 if(sender.equalsIgnoreCase("+44xxxxxx")
   this.abortBroadCast();

You also have to set it the high priority.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionumakantView Question on Stackoverflow
Solution 1 - AndroidChristopher OrrView Answer on Stackoverflow
Solution 2 - AndroidSunil Kumar SahooView Answer on Stackoverflow
Solution 3 - AndroidSubbaView Answer on Stackoverflow
Solution 4 - Androidtaran mahalView Answer on Stackoverflow
Solution 5 - AndroidPir Fahim ShahView Answer on Stackoverflow