Dynamic broadcast registration for SMS_RECEIVED is not working

自作多情 提交于 2019-12-12 03:56:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!