6

in my onReceive method I have this code:

if (from.equals(number)) {
    abortBroadcast();
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(in);
    Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(context, "Not from needed number", Toast.LENGTH_SHORT).show();
}   

where number = "29853" - number messages from which I wanna catch and not save in Inbox.

This code works correctly - if sms is from number the first Toast works and it prints the content of the message, if sms is not from number "Not from needed number" is printed. The problem is that abortBroadcast doesn't make its businness - the message from number is still in the Inbox of the phone although receiver's priority is 1000:

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

What's the problem - why doesn't abortBroadcast work?

Sergey
  • 11,548
  • 24
  • 76
  • 113

4 Answers4

8

move the android:priority to the intent-filter where it belongs.

 <intent-filter android:priority="9999999"> 

then you will more likely have priority and your canceling will work. I have tested it and it does work.

llogan
  • 121,796
  • 28
  • 232
  • 243
Pops
  • 81
  • 1
  • 2
  • what is the maximum value documented for priority ? did you calculate this value just by trial and error ? – Ahmed Dec 18 '12 at 16:38
  • The documented number is 1000. However everyone is using a number much higher, so basically the current situation is something like "first install first win". – superarts.org May 20 '13 at 03:17
  • 5
    According to the documentation: `The value must be an integer, such as "100". Higher numbers have a higher priority. The default value is 0. The value must be greater than -1000 and less than 1000.` So the maximum number exactly is 999. http://developer.android.com/guide/topics/manifest/intent-filter-element.html – GoRoS Sep 16 '13 at 14:00
4

From Android 4.4 you cannot abort these broadcasts. Here's a link to the AOSP sources that show this:

https://github.com/CyanogenMod/android_frameworks_opt_telephony/blob/d43b5b1ca91e0aac0c697546a5cb341ffa758e0b/src/java/com/android/internal/telephony/ImsSMSDispatcher.java#L605

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
  • Certain features are just too dangerous to allow any app to take control over them, so this is the right behavior. – andreszs Apr 10 '18 at 22:21
3

The abortBroadcast() does not DELETE messages from the inbox, it is just suppressing the status bar notification. To delete the sms from the inbox, refer to this link

Community
  • 1
  • 1
Zerhinne
  • 1,987
  • 2
  • 22
  • 43
1

I've used this a while ago and it was working, I could reject some messages to show-up but this doesn't work now. I think android no longer lets user take all control over SMS_RECEIVED event.

Cosar
  • 11
  • 3