Oreo BroadcastReceiver收到的SMS无效 [英] Oreo BroadcastReceiver SMS Received not working

查看:142
本文介绍了Oreo BroadcastReceiver收到的SMS无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的应用程序允许用户允许该应用程序读取确认SMS的内容,以自行输入验证码.对于使用早于Oreo(API 26)的OS的所有设备,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>

但是,从Oreo开始,必须将BroadcastReceivers显式注册到适当的上下文.我将其实现如下:

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.

在这里,我有几个问题:

Here, I have several questions:

  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开发者网站上描述的方式实例化了我的IntentFilter,即使用ConnectivityManager.CONNECTIVITY_ACTION操作.我知道SmsReceiver可以工作,因为onReceive()中的断点总是在注册接收器时命中.但是,该操作仅是CONNECTIVITY_ACTION.接收方永远不会捕获SMS_RECEIVED_ACTION.

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?

我是否还有其他遗漏之处,可能导致接收方无法捕获到到达的SMS?

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,然后它开始在奥利奥(Oreo)和我也在清单中指定了接收器,我不知道这是否有帮助,但这对我来说很重要.

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

        }

这篇关于Oreo BroadcastReceiver收到的SMS无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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