Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

android - BroadcastReceiver SMS_Received not working on new devices

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

The code below shows the basics:

Broadcast receiver class that displays toast onReceive

public class IncomingSms extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "something received", Toast.LENGTH_SHORT).show();
    }
}

Simple Manifest with registering receiver and permissions

<application
    <receiver 
        android:name=".IncomingSms"
        android:permission="android.permission.BROADCAST_SMS"
        android:exported="true">

        <intent-filter android:priority="2147483647" >
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

</application>

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />

The code above declares and registers the receiver, and has proper permissions. In addition, the priority is set to MAX_INT, or 2147483647.

My device is Nexus 6P, with default Messenger app installed (I also tried Hangouts). The app still does not display my toasts. After trying on an older Samsung device, the toasts were printed properly.

Priority issue

I installed on the 6P an app called Manifest Viewer, which allows me to see the manifest.xml of apps installed on my device. I checked the manifests of both Messenger and Hangouts, for the receiver of SMS tag, and found that both of them also specify a priority of 2147483647. It seems like both those messenger apps max out the priority, and once they consume the message, they don't allow other applications to intervene. Note that these are stock Google apps, and not 3rd party.

At this point, I am quite confused as to:

  • why would they do this?
  • how to bypass it?

Thanks a lot

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...