收到的奥利奥广播接收器短信不起作用 [英] Oreo BroadcastReceiver SMS Received not working

查看:45
本文介绍了收到的奥利奥广播接收器短信不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的一个应用程序允许用户允许该应用程序读取确认短信的内容以自行输入验证码.对于使用早于 Oreo (API 26) 的操作系统的所有设备,BroadcastReceiver 的实现可以正常工作并允许正确接收 SMS.通过这个实现,我的意思是将接收器对象放在 AndroidManifest 中.

An app I'm working on allows the user to allow the app to read the contents of a confirmation SMS to input the verification code on its own. For all devices using an OS earlier than Oreo (API 26), the implementation of the BroadcastReceiver works correctly and allows a proper reception of the SMS. By this implementation I mean placing the receiver object in the AndroidManifest.

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

然而,从奥利奥开始,必须将广播接收器显式注册到适当的上下文.我已经实现了如下:

However, starting with Oreo, one must explicitly register BroadcastReceivers to the appropriate context. I have implemented this as follows:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            smsReceiver = new SmsReceiver();
            IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
            intentFilter.addAction(Telephony.Sms.Intents.DATA_SMS_RECEIVED_ACTION);
            this.registerReceiver(smsReceiver, intentFilter);
        }

此代码块在收到 Manifest.permission.READ_SMS 的许可后执行.SmsReceiver 类扩展了 BroadcastReceiver 并覆盖了它的 onReceive() 方法.

This block of code is executed upon receiving permission for Manifest.permission.READ_SMS. The SmsReceiver class extends BroadcastReceiver and overrides its onReceive() method.

这里,我有几个问题:

  1. 我已经测试了这个实现,并在我的 SmsReceiver 中的 onReceive() 方法上设置了断点.当 SMS 到达时,应用程序永远不会进入 onReceive() 方法.为什么会这样?

  1. I have tested this implementation and have set breakpoints on my onReceive() method in my SmsReceiver. When an SMS arrives, the app never enters the onReceive() method. Why can this be?

我按照 Android Developer 网站上描述的方式实例化了我的 IntentFilter,即使用 ConnectivityManager.CONNECTIVITY_ACTION 操作.我知道 SmsReceiver 有效,因为 onReceive() 中的断点总是在接收器注册时被击中.然而,动作仅仅是 CONNECTIVITY_ACTION.SMS_RECEIVED_ACTION 永远不会被接收者捕获.是否绝对有必要使用此操作实例化 IntentFilter 或可以忽略此操作?

I instantiated my IntentFilter in the way it is described on the Android Developer website, i.e. with the ConnectivityManager.CONNECTIVITY_ACTION action. I know the SmsReceiver works, because the break point in onReceive() is always hit upon registration of the receiver. However, the action is merely the CONNECTIVITY_ACTION. The SMS_RECEIVED_ACTION is never caught by the receiver. Is it absolutely necessary to instantiate the IntentFilter with this action or can one leave this out?

还有什么我遗漏的东西会导致我的接收者没有收到到达的短信吗?

Is there something else I'm missing that could lead to my receiver not catching the arriving SMS?

推荐答案

以前我请求 -Manifest.permission.READ_SMS 没有用,然后我将权限更改为 - Manifest.permission.RECEIVE_SMS 然后它开始工作奥利奥和我还在清单中指定了接收器我不知道这是否有帮助,但这对我来说是美好的一天

Previously I was requesting for -Manifest.permission.READ_SMS which didn't worked then I changed the permissions to - Manifest.permission.RECEIVE_SMS then it started working in oreo and I also specified the receiver in manifest I don't know whether that helped or not but this made the day for me

   public static void requestPermissionForReadSMS(Fragment fragment) {
    //        if (fragment.shouldShowRequestPermissionRationale(Manifest.permission.READ_SMS)) {
    //            Helpers.showRequestPermissionAlertDialog(fragment, fragment.getString(R.string.read_sms_permission), fragment.getString(R.string.permission_request));

    //        } else {
            fragment.requestPermissions(new String[]{Manifest.permission.RECEIVE_SMS},
                    Constants.READ_SMS_PERMISSION);
   // }

        }

这篇关于收到的奥利奥广播接收器短信不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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