Android BroadcastReceiver

BroadcastReceiver

Android中的广播意味着一个发送广播,一个接受广播。那么怎样发送,发送的内容是什么?
1,广播的发送者。
在Android中,组件可以发送广播来传达消息。所以android就提供了一些列的方法来发送广播
public void sendBroadcast (Intent intent) 
是一个异步方法
intentThe Intent to broadcast; all receivers matching this Intent will receive the broadcast.
发送intent,在Intent中带有信息,接受广播的receiver有很多个,但是只有符合Intent条件的receiver才能接收这条广播消息。
public void sendBroadcast (Intent intent, String receiverPermission)
receiverPermission(optional) String naming a permissions that a receiver must hold in order to receive your broadcast. If null, no permission is required.


有序广播的发送
public void sendOrderedBroadcast (Intent intent, String receiverPermission)
public void sendOrderedBroadcast (Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)
receiverPermission   这是权限,一个接收器必须持以接收您的广播。如果为   null   ,不经许可的要求。  
resultReceiver
  您自己   BroadcastReceiver   来当作最后的广播接收器。  
调度自定义处理程序,用以安排   resultReceiver   回调   ;   如果为   null   将语境中的主线程举行。  
initialCode
  一种结果代码的初始值。通常为   Activity.RESULT_OK   。这个值是   -1   ;为其他   int     也可以,如 0,1,2    
initialData
  一种结果数据的初始值。通常情况下为空   ,     String   类型   ;
initialExtras
  一种结果额外的初始值。通常情况下为空   ,     Bundle;
异步广播的发送和接收
public void sendStickyBroadcast (Intent intent)
public void sendStickyOrderedBroadcast (Intent intent, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)
intentThe Intent to broadcast; all receivers matching this Intent will receive the broadcast.
resultReceiverYour own BroadcastReceiver to treat as the final receiver of the broadcast.
schedulerA custom Handler with which to schedule the resultReceiver callback; if null it will be scheduled in the Context's main thread.
initialCodeAn initial value for the result code. Often Activity.RESULT_OK.
initialDataAn initial value for the result data. Often null.
initialExtrasAn initial value for the result extras. Often null.
发送 这个广播需要   <uses-permission android:name="android.permission.BROADCAST_STICKY" />这个权限
代码中注册广播:

注册广播方法一: registerReceiver(BroadcastReceiver receiver, IntentFilter filter) ,第一个参数是我们要处理广播的 BroadcastReceiver (广播接收者,可以是系统的,也可以是自定义的);第二个参数是意图过滤器。

 

注册广播方法二: registerReceiver(receiver, filter, broadcastPermission, scheduler) ,第一个参数是BroadcastReceiver (广播接收者,可以是系统的,也可以是自定义的);第二个参数是意图过滤器;第三个参数是广播权限;第四个参数是 Hander ;

 

注意:权限重复现象,如果功能清单文件里注册了权限,在该方法再注册,则 receiver 无法收到广播,如果 功能清单文件里没有注册了权限,该方法注册也无法收到。当该方法没有注册权限,功能清单里注册的时候,receiver 能收到广播。

总结:在   Activity   中代码注册广播建议在:   onResume()   中注册;
广播注销

// 代码中注销广播

/unregisterReceiver(mBatteryInfoReceiver);

在 Activity 中代码注销广播建议在: onPuase() 中注销;

不要这这里面注销 Activity.onSaveInstanceState(), 因为这个方法是保存 Intent 状态的。

广播的接收端

广播发出去了,要接收然后作出反应才行,BroadcastReceiver就是来接受广播的
通过继承BroadcastReceiver来实现广播接收器。BroadcastReceiver类有两个方法
public void onReceive(Context context, Intent intent)
一般只需要实现这个接受方法就OK了
intent:从发送端发送过来的intent对象。

public IBinder peekService(Context myContext, Intent service)

BroadcastReceiver的常用API有:

abortBroadcast ():

这个方法可以截获由 sendOrderedBroadcast () 发送来的 广播,让其它广播接收者无法收到这个广播。

clearAbortBroadcast ()

这个方法是针对上面的 abortBroadcast() 方法的,用于取消截获广播。这样它的下一级广播接收者就能够收到该广播了。

getAbortBroadcast ()

这个方法作用是:判断是否调用了 abortBroadcast (),如果先调用 abortBroadcast (),接着再调用getAbortBroadcast (),将返回 true; 如果在调用 abortBroadcast() 、 clearAbortBroadcast ()

getAbortBroadcast (),将返回 false;

 

public final boolean getDebugUnregister ()

Since: API Level 1

Return the last value given to setDebugUnregister(boolean) .

 

 

getResultCode ()

如果用下面四个方法发送得广播,返回码为: -1 ;

// sendBroadcast(intent);

// sendBroadcast(intent, receiverPermission);

// sendOrderedBroadcast(intent, receiverPermission);

// sendStickyBroadcast(intent);

如果用下面两个方法发送得广播,返回码为:根据你设置 initialCode 的数字是多少就是多少;

// sendStickyOrderedBroadcast(intent, resultReceiver, scheduler,

// initialCode, initialData, initialExtras)

// sendOrderedBroadcast(intent, receiverPermission, resultReceiver,

// scheduler, initialCode, initialData, initialExtras)

 

 

getResultData ()

得到发送广播时设置的 initialData 的数据;

 

 

getResultExtras (boolean makeMap)

If true then a new empty Map will be made for you if the current Map is null; if false you should be prepared to receive a null Map.

得到由

sendStickyOrderedBroadcast(intent, resultReceiver, scheduler,

// initialCode, initialData, initialExtras) ;

 

// sendOrderedBroadcast(intent, receiverPermission, resultReceiver,

// scheduler, initialCode, initialData, initialExtras)

中 initialExtras 传入的参数。

实验:我用上面两个方法发了 initialExtras (这个一个 Bundle )传入的参数时,只要不为空,那么 makeMap是否为 true 和 false 都能够得到数据。

 

isInitialStickyBroadcast ()

Returns true if the receiver is currently processing the initial value of a sticky broadcast -- that is, the value that was last broadcast and is currently held in the sticky cache, so this is not directly the result of a broadcast right now.

 

如果广播接收者是目前处理的一个宿主的广播的初始值,将返回 true , - 也就是说,这个值是最后的广播出的值,目前正在举行的宿主缓存,所以这并不是直接导致了现在的广播。

 

实验:在第三个应用中调用这个方法,无论你用哪种方式发送广播,这个方法得到的总是 false ;在发送广播的 resultReceiver 广播接收者里面调用,得到的也是 false ;

 

 

isOrderedBroadcast ()

sendStickyOrderedBroadcast(intent, resultReceiver, scheduler,

  initialCode, initialData, initialExtras)

上面这个方法发送时,得到的是 true;

判断是否是有序广播;

public IBinder peekService (Context myContext, Intent service)

Provide a binder to an already-running service. This method is synchronous and will not start the target service if it is not present, so it is safe to call from onReceive.

 

Parameters:

myContext The Context that had been passed to onReceive(Context, Intent)

service The Intent indicating the service you wish to use. See Context.startService(Intent) for more information.

setDebugUnregister (boolean debug)

Control inclusion of debugging help for mismatched calls to {@ Context#registerReceiver(BroadcastReceiver, IntentFilter) Context.registerReceiver()}. If called with true, before given to registerReceiver(), then the callstack of the following Context.unregisterReceiver()call is retained, to be printed if a later incorrect unregister call is made. Note that doing this requires retaining information about the BroadcastReceiver for


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值