English 中文(简体)
Can we delete an SMS in Android before it reaches the inbox?
原标题:
  • 时间:2009-11-16 11:33:43
  •  标签:
  • android
  • sms

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

问题回答

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

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

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.

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

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 - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签