简体   繁体   中英

BroadcastReciever for SMS_RECEIVED_ACTION registered in manifest not working

Registering an SMS broadcast receiver in the Manifest doesn't seem to work anymore.

I'm aware that Android recently locked down the manifest broadcast receivers however the SMS_RECEIVED_ACTION appears to be on the exception list .

My receiver looks like this:

class SmsReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action != SMS_RECEIVED_ACTION) return
        val messages = getMessagesFromIntent(intent)
        for (s in messages) {
            Log.i("SmsReceiver", s.displayMessageBody)
            Toast.makeText(context, s.displayMessageBody, Toast.LENGTH_LONG).show()
        }
    }
}

And registered in the manifest:

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />

    <application
        ...
        tools:targetApi="33"
        android:enabled="true">
        <receiver
            android:name=".SmsReceiver"
            android:enabled="true"
            android:exported="false"
            android:permission="android.permission.BROADCAST_SMS,android.permission.RECEIVE_SMS">
            <intent-filter android:priority="2147483647">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

As you can see, I've been combining tricks from SO for a while and none have proved fruitful as yet. Hopefully it's something stupid, help appreciated:)

Delete android:permission="android.permission.BROADCAST_SMS,android.permission.RECEIVE_SMS" .

There is no permission named android.permission.BROADCAST_SMS,android.permission.RECEIVE_SMS . android:permission does not take a comma-delimited list. And, you do not need that attribute.

I guess you should change android:exported="true"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM