BroadcastReceiver SMS_Received在新设备上不起作用 [英] BroadcastReceiver SMS_Received not working on new devices

查看:82
本文介绍了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:

广播接收器类,在onReceive上显示吐司

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标记和环聊的清单作为SMS标记的接收者,发现它们都还指定了2147483647的优先级.似乎这两个Messenger应用程序都将优先级最大化,一旦他们使用了该消息,它们就会不允许其他应用程序干预.请注意,这些是库存的Google应用程序,而不是第三方.

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类的顶部定义此名称:

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天全站免登陆