0

I'm trying to intercept the received SMSs by using a broadcast receiver. Here is the code:

    <receiver android:name=".receivers.SmsReceiver" android:enabled="true"
        android:exported="true" android:priority="999">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

and:

public class SmsReceiver extends BroadcastReceiver {
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

@Override
public void onReceive(Context context, Intent intent) {
    if (SMS_RECEIVED.equals(intent.getAction())) {
        this.abortBroadcast();

        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            // get sms objects
            Object[] pdus = (Object[]) bundle.get("pdus");
            if (pdus.length == 0) {
                return;
            }
            // large message might be broken into many
            SmsMessage[] messages = new SmsMessage[pdus.length];
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                sb.append(messages[i].getMessageBody());
            }
            String sender = messages[0].getOriginatingAddress();
            String message = sb.toString();

            Log.d("sms", sender);
            Log.d("sms", message);
        }
    }
}
}

The SMS is intercepted fine, but the stock Android SMS app is still showing its notifications and I can also find the message inside the stock app sms list.

Is there any way to stop the stock SMS app notifications and to avoid the message from appearing inside its list?

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
iGio90
  • 3,251
  • 7
  • 30
  • 43

3 Answers3

0

You need to call abortBroadcast();, see my answer to communication between two device using sms

If you are running Android 4.4 KitKat, it seems to be more difficult to do these sorts of things and have not looked into it yet myself.

Community
  • 1
  • 1
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • Yeah, as shown in my code, I'm calling it! and yeah... I'm running kitkat! – iGio90 Jan 05 '14 at 18:15
  • Sorry did not notice but yeah, then it is not your code but the changes in KitKat that you are facing issues with. Please report back if you find the answer. – cYrixmorten Jan 05 '14 at 18:16
  • Sure, if i find a way i'll add an answer here and tag you! thanks for support meanwhile! if you find a solution before me then, do the same please :D – iGio90 Jan 05 '14 at 18:18
0

As you said you are running KitKat, then answer is - you cannot mute default SMS app. You can also receive messages or send (that's why you get messages), but still, you cannot "consume" the message.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Hello Marcin. Well ok (damn it). Maybe you know if we can edit the data such as sender and message before the stock app get it? so that the message app will show custom data instead of the original one? – iGio90 Jan 05 '14 at 18:26
  • No, you cannot tamper the messages. – Marcin Orlowski Jan 05 '14 at 18:27
0

If user has set your application as default SMS app, then he/she is not going to get an SMS notification. You have to handle the notification, as well as other feature of the SMS in your app.

For more information read this blog.

Check out other blog and sample app.

SkyWalker
  • 855
  • 2
  • 14
  • 36