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

5

100

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

Franks answered 16/11, 2009 at 11:33 Comment(7)
Is j2ee really a matching tag here?Tynishatynwald
Why would java be incorrect don't you program in java on Android?Tynishatynwald
This reeks of evil. You shouldn't be doing this.Florey
actually i am not going for any evil its only a optional to make customer relief from unwanted distrurbanceFranks
There are legitimate uses! For example if you want to do something (send GPS location, wipe or something) on your phone via SMS in case it is stolen; you don't want whoever who stole the phone to see that SMS message.Limoli
@polyglot: There may be legit uses, but there are evil full on fraudulent uses too--uses that steal money from people. EVIL (yes, caps and bold)Presentable
Come on people! This is definitely NOT "evil" at all. @Limoli is right! I'm doing an application to confirm the user's number (as Viber do) and I need it. Everything can be used as "evil". Don't install evil apps, just that.Repercussion
D
187

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

Dickman answered 2/4, 2010 at 10:42 Comment(9)
Holy god. This is true! I have always assumed it would be the other way around! For proof, look here. Wow. Wish I could give you more than one upvote ;)Fabrianna
@kakopappa: Yes, as mentioned in the answer it works from Android 1.6+.Dickman
Thanks it works!! If you still receiving notifications, try restarting the phone.Richardricharda
Hey, this also worked for me until I installed the Handscent SMS app on my SGS2, now I'm not able to retrieve SMS anymore as Handscent is catching them before... I tried to set android:priority to 0 1 and even 999 (as this seems to be the last number accepted) but I couldn't figure it out - has anybody had the same problem and eventually a solution for it?Kiesha
@Kiesha I have apps which use priority 1000, so it is possible. In fact, Handcent are using priority 2147483647.Dickman
This worked for me to :) My app catch message first and there is not notification but then sms is not getting to Messages inbox. Like it was never received, only in my app. Is there a way to use abort broadcast but to receive messages in inbox?Education
It's important to bear in mind the new API level 19 as @ChristopherOrr says in his update. There it says "Beginning with Android 4.4—any attempt by your app to abort the SMS_RECEIVED_ACTION broadcast will be ignored so all apps interested have the chance to receive it.".Sikata
Can you please see here #25989074 ?Eurhythmic
Regarding the example of intercepting SMS for phone number verification, here's the proper way to do that: developers.google.com/identity/sms-retriever/requestPergolesi
B
11

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

Burleson answered 25/6, 2013 at 13:29 Comment(1)
can we have a broadcast receiver ,when the user presses message inbox,so that i can develop a lock screen ..Shamus
K
9

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.

Koestler answered 26/7, 2011 at 23:59 Comment(7)
Good point. But this should be a comment, not an answer. Is it possible to move it? --- and what if you put a very high value? As android:priority="9999" ? for example? Have you tried that?Repercussion
@FelipeMicaroniLalli actually GO SMS PRO, etc... use 2147483647 for their priorityLafayette
@DDoSAttack 2147483647 is the maximum?Repercussion
It is the largest unsigned integer en.wikipedia.org/wiki/Integer_(computer_science) the max that Android supports is 999. developer.android.com/reference/android/content/…Lafayette
@DDoSAttack but max priority is 999.. how they can use this figure that u had mentioned?Octave
@NomanHamid That is correct. 999 is the max priority. You can use whatever number you choose, Android will only guarantee up to 999 though. Anything larger than that and it may or may not be unstable and have unintended consequences. Personally, I had to use 2147483647 to try to go over apps like GO SMS PRO. Sometimes it worked for users and sometimes it didn't.Lafayette
@DDoSAttack Alright i got your point but it will be good if we follow guidelines :-) ... i have used 999 and its working fine for me in all cases. I appreciate you explanation.Octave
G
1
/**
 * 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.

Gorga answered 12/5, 2014 at 18:36 Comment(0)
A
0

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.

Acetic answered 10/12, 2012 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.