BroadcastReceiver SMS_Received not working on new devices

后端 未结 2 1749
一整个雨季
一整个雨季 2020-12-01 18:50

After going through several resources and questions, I still face a problem with detecting an incoming SMS message.

The code below shows the basics:

相关标签:
2条回答
  • 2020-12-01 19:24

    Please make sure your implementation is like this.

    SMS Receiver class

        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 (intent.getAction().equals(SMS_RECEIVED)) {
                    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();
                        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                        // prevent any other broadcast receivers from receiving broadcast
                        // abortBroadcast();
                    }
                }
            }
        }
    

    Manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.smsreceiver"
        android:versionCode="1"
        android:versionName="1.0">
        <uses-sdk android:minSdkVersion="4" />
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity
               // your activity
            </activity>
            <receiver android:name="com.example.smsreceiver.SmsReceiver" android:enabled="true">
                <intent-filter android:priority="2147483647">
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>
        </application>
    </manifest>
    

    Make sure you use your own defined package. package defined here is dummy.

    0 讨论(0)
  • 2020-12-01 19:45

    Okay the problem was resolved. The issue did not reside with priorities, but with my phone being a Nexus 6P (a.k.a. API 23).

    Providing permissions in the manifest.xml alone wasn't enough and I had to add code for runtime permission request. See Android documentation for runtime permissions

    Add this code to your MainActiviy:

    ActivityCompat.requestPermissions(this, 
                new String[]{Manifest.permission.RECEIVE_SMS},
                MY_PERMISSIONS_REQUEST_SMS_RECEIVE);
    

    Define this at the top of MainActivity Class:

    private int MY_PERMISSIONS_REQUEST_SMS_RECEIVE = 10;
    

    And also add this override:

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == MY_PERMISSIONS_REQUEST_SMS_RECEIVE) {
            // YES!!
            Log.i("TAG", "MY_PERMISSIONS_REQUEST_SMS_RECEIVE --> YES");
        }
    }
    
    0 讨论(0)
提交回复
热议问题