5.1廣播機(jī)制簡介
廣播分為標(biāo)準(zhǔn)廣播和有序廣播
- 標(biāo)準(zhǔn)廣播:異步執(zhí)行廣播,廣播發(fā)出后,所有的廣播接收器都會幾乎在同時接收到,沒有先后順序.效率較高,但是無法攔截.
- 有序廣播:同步執(zhí)行廣播,同一時刻只有一個廣播接收器能夠接收到該廣播,且僅當(dāng)該接收器的邏輯執(zhí)行結(jié)束后再回繼續(xù)傳遞.優(yōu)先級高的先收到廣播消息(同等優(yōu)先級則隨機(jī)選擇一個),前面的接收器可以截斷正在傳播的廣播.
5.2接收系統(tǒng)廣播
廣播接收器可以對自己感興趣的廣播進(jìn)行注冊.注冊廣播分為靜態(tài)注冊和動態(tài)注冊.
- 動態(tài)注冊:在代碼中注冊.
- 靜態(tài)注冊:在AndroidManifest.xml中注冊.
onReceive()內(nèi)不允許添加過多邏輯或耗時操作.通常半夜打開其他程序或組件的角色.
- 廣播接收器中不允許開線程.
- onReceive()運行時間過程而沒有結(jié)束時,程序會報錯.
5.2.1動態(tài)注冊監(jiān)聽網(wǎng)絡(luò)變化
步驟:
1.新建一個廣播接收器內(nèi)部類.
2.繼承BroadcastReceiver.
3.重寫父類的onReceive(),intenFilter中寫監(jiān)聽的廣播.
注意:
- 僅在軟件啟動后才有作用.
- onDestroy()中加上unregiReceive(),用于銷毀接收器.
示例:
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangeReceiver = new NetworkChangeReceiver();
registerReceiver(networkChangeReceiver,intentFilter);
5.2.2靜態(tài)注冊實現(xiàn)開機(jī)啟動
步驟:
1.新建廣播接收器類.
2.繼承BroadcastReceiver.
3.AndroidManifest.xml中注冊,注冊中填寫監(jiān)聽的廣播.
注意:
- 該方法可以實現(xiàn)開機(jī)自啟
- 注冊在<application>內(nèi)
示例:
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
5.3發(fā)送自定義廣播
5.3.1發(fā)送標(biāo)準(zhǔn)廣播
步驟:
1.new Intent,參數(shù)為廣播名.
2.sendBroadcast(intent).
注意:
- 可以在intent中傳遞一些數(shù)據(jù)被廣播接收器.
示例:
Intent intent = new Intent("com.wjoker.broadcasttest.MY_BROADCAST");
sendBroadcast(intent);
5.3.2發(fā)送有序廣播(和截斷廣播
步驟:
1.發(fā)送廣播使用sendOrderBroadcast().
2.在注冊接收器處加權(quán)限.
3.onReceive()處使用abortBroadcast().(截斷)
步驟:
Intent intent = new Intent("com.wjoker.broadcasttest.MY_BROADCAST");
sendOrderedBroadcast(intent,null);
<receiver android:name=".MyBroadcastReceive">
<intent-filter android:priority="100">
<action android:name="com.wjoker.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"test",Toast.LENGTH_SHORT).show();
abortBroadcast();
}
5.4使用本地廣播
用于解決安全性問題.只接受本應(yīng)用內(nèi)的廣播或只在本應(yīng)用內(nèi)傳遞.
步驟:
1.通過LocalBroadcastManager對廣播進(jìn)行管理(發(fā)送/接收).
注意:
- 本地廣播無法通過靜態(tài)方式注冊.
- 比系統(tǒng)全局廣播效率更高.
示例:
private LocalBroadcastManager localBroadcastManager;
localBroadcastManager = LocalBroadcastManager.getInstance(this);
Intent intent = new Intent("com.wjoker.broadcasttest.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent);//發(fā)送本地廣播
intentFilter = new IntentFilter();
intentFilter.addAction("com.wjoker.broadcasttest.LOCAL_BROADCAST");
localReceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver, intentFilter);// 注冊本地廣播監(jiān)聽器
總結(jié)
1.廣播根據(jù)是否存在接收順序可分為標(biāo)準(zhǔn)廣播和有序廣播;根據(jù)范圍可分為全局廣播和本地廣播.
2.有序廣播可以截斷廣播;本地廣播是安全且高效的.
3.廣播接收器注冊分為動態(tài)注冊(代碼中注冊,啟動后開始監(jiān)聽)和靜態(tài)注冊(AndroidManifest.xml中注冊,隨時監(jiān)聽).
4.onReceive()中不允許開多線程且運行過長會報錯.
5.動態(tài)注冊的監(jiān)聽內(nèi)容在interFace.addAction()中添加,靜態(tài)注冊在<action>中添加.
6.要銷毀接收器.
7.發(fā)送標(biāo)準(zhǔn)廣播,使用sendBroadcast(intent).
8.發(fā)送有序廣播使用sendOrderBroadcast(intent),要在接收器里設(shè)置優(yōu)先級.
9.使用abortBroadcast()截斷廣播(僅有序).
10.本地廣播使用LocalBroadcastManager管理(接收/發(fā)送).