0

Blocking SMS but it doesn't seem to work on Android version 4.4 and higher. This has been discussed elsewhere on Stackoverflow, but no known workaround so far. Is there anything that can be done to have a priority greater than the default SMS app? Here the code I use :

    if (MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) {
//          Toast toast = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG);
//          toast.show();

            Bundle bundle = intent.getExtras();
            Object messages[] = (Object[]) bundle.get("pdus");
            SmsMessage smsMessage[] = new SmsMessage[messages.length];
            for (int n = 0; n < messages.length; n++) {
                smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
            }
            String body = smsMessage[0].getMessageBody().toString();
            String num = smsMessage[0].getOriginatingAddress().toString();
            try {
                for (String a : mBlackList) {
                    if (PhoneNumberUtils.compare(a, num)) {
                        abortBroadcast();
                        if (preferences.getBoolean("show_notif", true)) {
                            showNotification(num, body);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
  • "Is there anything that can be done to have a priority greater than the default SMS app?" -- become the user's default SMS app. http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html – CommonsWare Feb 25 '15 at 23:11
  • @CommonsWare I was aware of that, but that's not an option, since my app is not meant to be sending and receiving messages, but merely just blocking. –  Feb 25 '15 at 23:13
  • 1
    Then your app simply will not work on Android 4.4+. – CommonsWare Feb 25 '15 at 23:14
  • @CommonsWare, is it also not possible to delete messages from inbox? Could that work as a workaround? To delete as soon as the message is received? –  Feb 25 '15 at 23:16
  • "is it also not possible to delete messages from inbox?" -- correct. Only the user's chosen SMS client can have write access to the SMS provider. "while your app is not selected as the default, it's important that you understand the limitations placed upon your app and disable features as appropriate. Although the system writes sent SMS messages to the SMS Provider while your app is not the default SMS app, it does not write sent MMS messages and your app is not able to write to the SMS Provider for other operations, such as to mark messages as draft, mark them as read, delete them, etc." – CommonsWare Feb 25 '15 at 23:17
  • @CommonsWare, thank you for clearing that up. –  Feb 25 '15 at 23:18
  • @MikeM. How'd you go about implementing? I am very much interested. –  Feb 25 '15 at 23:42
  • @MikeM, thanks I'll take a look at make a comment there depending on how it goes. What causes it to not work on Lollipop? –  Feb 25 '15 at 23:51

1 Answers1

1

This is due to the changes to SMSProvider in 4.4. To abort broadcasts your app must be registered as the "default SMS app."

Other apps that are not selected as the default SMS app can only read the SMS Provider, but may also be notified when a new SMS arrives by listening for the SMS_RECEIVED_ACTION broadcast, which is a non-abortable broadcast that may be delivered to multiple apps. This broadcast is intended for apps that---while not selected as the default SMS app---need to read special incoming messages such as to perform phone number verification.

https://developer.android.com/about/versions/android-4.4.html#SMS

Details about getting set up as the default SMS app:

http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html

Michael Powell
  • 746
  • 4
  • 11
  • Yeah, I read up on that. I've been working on hacks, but nothing seems to work. The only option is to make the app a default SMS app, but that is not a viable option for me. –  Feb 25 '15 at 23:14
  • @Michael "To abort broadcasts your app must be registered as the 'default SMS app.'" - Not even the default SMS app can abort the `SMS_RECEIVED` broadcast. – Mike M. Feb 26 '15 at 00:07