Hide sms notifications with a broadcastreceiver in KitKat

做~自己de王妃 提交于 2019-12-09 07:20:16

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/20937213/hide-sms-notifications-with-a-broadcastreceiver-in-kitkat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!