BroadcastReceiver相信每个android开发者都是接触过的,今天不讲它的用法,而是说说有序广播与无序广播的特点
在多数代码中我们经常会看到sendBroadcast(),使用这样的方式都是发送一份无序广播,而使用sendOrderedBroadcast()则是发送一份有序广播,而接收广播的先后顺序在BroadcastReceiver中的intent-filter中配置prioity的值,这个值越大,接收广播的优先级越高。
在判断一个广播是不是有序广播的简单方式就是通过abortBroadcast()方法判断,如果此广播被终止了,则说明则广播为有序广播,因为无序广播是无法终止的。
在这里还简单说一个方法:
public abstract void sendOrderedBroadcast (Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)
ntent | The Intent to broadcast; all receivers matching this Intent will receive the broadcast. |
---|---|
receiverPermission | String naming a permissions that a receiver must hold in order to receive your broadcast. If null, no permission is required. |
resultReceiver | Your own BroadcastReceiver to treat as the final receiver of the broadcast. |
scheduler | A custom Handler with which to schedule the resultReceiver callback; if null it will be scheduled in the Context's main thread. |
initialCode | An initial value for the result code. Often Activity.RESULT_OK. |
initialData | An initial value for the result data. Often null. |
initialExtras | An initial value for the result extras. Often null. |