Aborting/Cancelling Broadcasts

萝らか妹 提交于 2019-12-18 16:52:18

问题


What I want: I want to be the first to receive the Sms Broadcast and I want to cancel the broadcast if SMS is of my interest only, so that The broadcast doesn't reach any other app/receiver (Default messaging app etc.). What I know is:

  • SmsDisptacher.java uses orderedBroadcasts that can be can canceled/aborted.

What I don't know is:

  • If orderedBrodcasts can be canceled for other apps/receivers i.e other than yourself.

what I have tried for being the first to receive the Broadcast:

  • intent-filter android:priority="1000"

What i have tried for canceling broadcast already:

  • AbortBroadcast();
  • broadcastReceiver.setResultCode(RESULT_CANCELED)
  • clearAbortBroadcast()

回答1:


It is certainly possible. Register your receiver in your manifest like this:

<receiver android:name=".SmsReceiver">
    <intent-filter android:priority="1000">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

In your receiver's onReceive method write:

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(SMS_RECEIVED)) {
        //do your stuff
        abortBroadcast();
    }
}

Remember to set the permission to receive sms messages in the manifest like this:

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

Without the correct permission set it will fail silently with the following logcat message:

07-13 12:54:13.208: WARN/ActivityManager(201): Permission Denial: receiving Intent { act=android.provider.Telephony.SMS_RECEIVED (has extras) } to com.mypackage requires android.permission.RECEIVE_SMS due to sender com.android.phone (uid 1001)

Finally, it is possible that another application has registered a receiver with an even higher priority than yours, and that it therefore receives the broadcast before you cancel it. So if all else fails, try experimenting with higher priority values.




回答2:


it's possible to do that, i did a tutorial check it please here and here just abort the broadcast on the top of the hierarchy




回答3:


Got my answer http://groups.google.com/group/android-developers/browse_thread/thread/78fecbc156f4a1ea Its not possible.



来源:https://stackoverflow.com/questions/5191020/aborting-cancelling-broadcasts

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