我无法在Android Oreo中收到“android.provider.Telephony.SMS_RECEIVED”这个广播。

6

这是我的清单文件

<receiver
android:name="com.agribazaar.android.receivers.OTPReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

这是我的广播接收器类

public class OTPReceiver extends BroadcastReceiver {
   @Override
public void onReceive(Context context, Intent intent) {        
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){

}
}

你把你的应用设置为默认短信应用了吗? - AGM Tazim
2
请查看https://dev59.com/HaXja4cB1Zd3GeqPTpzZ - Paresh Mayani
不,它不是默认的短信应用程序。我只需要用于OTP的短信。 - Danish Farooq
但是你应该将其设置为默认短信应用程序,否则它将无法工作。它能在Kitkat、Lollipop和Marshmallow上运行吗? - AGM Tazim
是的,它在KitKat、Lollipop和Marshmallow上都能正常工作。 - Danish Farooq
1个回答

7
这对我非常有帮助 - 我没有明确在运行时请求android.Manifest.permission.RECEIVE_SMS权限。在早期版本的Android中它可以正常工作,但在Android O设备上会出现问题。
int SMS_PERMISSION_REQ_CODE_SUBMIT = 101;
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECEIVE_SMS)
                            != PackageManager.PERMISSION_GRANTED){

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接