问题
Registering broadcast receiver for SMS_RECEIVED
action in AndroidManifest.xml
file
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
works as expected, SmsReceiver.onReceive(Context context, Intent intent)
is called.
Trying to register dyamically
Intent inte = registerReceiver(
new SmsReceiver(),
new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION));
SmsReceiver.onReceive(Context context, Intent intent)
is never called.
notice
- Device API Level is LOWER than 23 so it's not runtime request permission problem
- tried
IntentFilter.setPriority(1000)
with no success. registerReceiver
returns null
How can i fix that ?
回答1:
How can i fix that ?
Start by using the correct Intent
action.
There are two action strings related to SMS delivery. What works for any app — and what you are using in the <intent-filter>
— is SMS_RECEIVED_ACTION. What only works for the user's chosen SMS client — and what you are trying to use with the IntentFilter
— is SMS_DELIVER_ACTION. These are not the same.
来源:https://stackoverflow.com/questions/42113584/dynamic-broadcast-registration-for-sms-received-is-not-working