BroadcastReceiver SMS_Received 不适用于新设备 [英] BroadcastReceiver SMS_Received not working on new devices

查看:27
本文介绍了BroadcastReceiver SMS_Received 不适用于新设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查阅了多个资源和问题后,我仍然面临检测传入 SMS 消息的问题.

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

下面的代码显示了基础知识:

The code below shows the basics:

在接收时显示 toast 的广播接收器类

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

带有注册接收器和权限的简单清单

<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" />

上面的代码声明并注册了接收者,并有适当的权限.另外,优先级设置为MAX_INT,即2147483647.

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

我的设备是 Nexus 6P,安装了默认的 Messenger 应用(我也尝试过环聊).该应用程序仍然不显示我的祝酒词.在较旧的三星设备上尝试后,吐司打印正确.

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.

优先事项

我在 6P 上安装了一个名为 Manifest Viewer 的应用程序,它允许我查看安装在我设备上的应用程序的 manifest.xml.我检查了 Messenger 和 Hangouts 的清单,对于 SMS 标签的接收者,发现它们也指定了 2147483647 的优先级.似乎这两个 Messenger 应用程序都最大化了优先级,一旦它们消费了消息,它们不允许其他应用程序干预.请注意,这些是现有的 Google 应用,而不是第 3 方.

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:

  • 他们为什么要这样做?
  • 如何绕过它?

非常感谢

推荐答案

好的,问题解决了.问题不在于优先级,而在于我的手机是 Nexus 6P(又名 API 23).

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).

仅在 manifest.xml 中提供权限是不够的,我必须为运行时权限请求添加代码.请参阅 Android 文档以了解运行时权限

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

将此代码添加到您的 MainActiviy:

Add this code to your MainActiviy:

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

在 MainActivity Class 的顶部定义:

Define this at the top of MainActivity Class:

private int MY_PERMISSIONS_REQUEST_SMS_RECEIVE = 10;

并添加此覆盖:

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

这篇关于BroadcastReceiver SMS_Received 不适用于新设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆