Broadcast Receiver not working for SMS

99封情书 提交于 2019-11-28 11:27:04
Moh Sakkijha

Try declaring your receiver as the following :

<receiver android:name=".SmsReceiver" android:permission="android.permission.BROADCAST_SMS" android:exported="true">
    <intent-filter android:priority="5822" >
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
 </receiver>

this works just fine for me , I only added a flag to tell that this receiver is exported.

Edit: I forgot to add the priority to the intent filter. use high number for the priority.

Pmsc

Found a topic that answers my doubt: Suppress / Block BroadcastReceiver in another app.

Even with the priority set to the maximum possible (999), if another app has the same priority, in this case the Handcent SMS app, the first application that will receive the broadcast is the one that was first installed by the user.

In my case was the Handcent SMS and because it aborts the broadcast when receiving it, my app doesn't receive anything.

I have tried everything and it worked on many phones but not mine,I tried to ask for permission in rum time (I have nexus 5 version 23). it worked for me.

As @Maxim Toyberman said, you need to ask for permission at runtime (as explained here https://stackoverflow.com/a/35972161/3427883 )

basically you need to make sure you have the permission to Receive the SMS ass following

        if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.RECEIVE_SMS}, 1);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!