简体   繁体   中英

Intercept incoming SMS samsung - Android

I'm intercepting SMS using the following code in Java

public class SmsReceiver extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {

            Bundle extras = intent.getExtras();
            if (extras == null)
                   return;


            Object[] pdus = (Object[]) extras.get("pdus");
            for (int i = 0; i < pdus.length; i++) {
                   SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
                   String sender = SMessage.getOriginatingAddress();
                   String body = SMessage.getMessageBody().toString();
                    Log.i("TAG", body);
                   //... do whatever with the message here


            }
     }
}

with the following receiver in the Manifest.xml

</receiver>
<receiver android:name="com.example.test.SmsReceiver" >
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

It's working fine with Nexus 5, but it's not in Samsung, anyone knows how to make it work in Samsung

Pre-KitKat SMS broadcasts could be intercepted or even aborted, but Hangouts will still process the message. See this post:

Suppress / Block BroadcastReceiver in another app

And this:

Enabling SMS support in Hangouts 2.0 breaks the BroadcastReceiver of SMS_RECEIVED in my app

Hangouts has registered their AbortSmsRecevier with a priority "3" so setting your receiver priority above "3" should solve your problem and intercept it. However, if you trying to get the message "first" then "999" should do that. However, be aware that this is not ideal since anti-spam apps, for example, may need to process the message before your apps does, depending on what you app does. (This problem - apps "fighting for highest priority" is the reason Android changed with KitKat, for better or for worse...)

This is the excerpt from the Google Talk / Babel manifest:

    <receiver android:name="com.google.android.apps.babel.sms.AbortSmsReceiver" android:permission="android.permission.BROADCAST_SMS" android:enabled="false">
        <intent-filter android:priority="3">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

If you are trying to abort the message, then you will have problems. The Hangouts SMS receiver that processes the message is set to Integer.MAX_VALUE , but the receiver that aborts the message is the one I just posted.

Here is the other receiver:

    <receiver android:name="com.google.android.apps.babel.sms.SmsReceiver" android:permission="android.permission.BROADCAST_SMS" android:enabled="false">
        <intent-filter android:priority="2147483647">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

Note: priorities over "999" or below "-999" are "system level" - however, the documentation states that non-system apps requesting a priority above this will have "unpredictable" behavior. Which is definitely what I've seen - apps will not predictably or reliable "beat" other apps above that level (depending on the device, install order, rebooting, etc.).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM