Can we delete an SMS in Android before it reaches

2018-12-31 17:27发布

问题:

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

回答1:

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



回答2:

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



回答3:

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.



回答4:

/**
 * 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.



回答5:

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.



标签: android sms