前言
BroadcastReceiver(廣播接收器) 是 Android 的四大組件之一,主要用于進程/線程間通信。
最大特點就是發(fā)送方并不關心接收方是否接到數(shù)據(jù),也不關心接收方是如何處理數(shù)據(jù)的,只負責「說」而不管接收方「聽不聽」。
Android開發(fā)中,廣播可來自系統(tǒng),如:Android 系統(tǒng)在發(fā)生各種系統(tǒng)事件時會發(fā)送廣播(系統(tǒng)啟動或者設備開始充電的廣播信息);也可來自于其他應用程序,如:應用程序可以發(fā)送自定義廣播,來通知其他應用程序接收可能感興趣的內容(更新數(shù)據(jù)等)
廣播分類
依據(jù)發(fā)送方式分類
-
標準廣播
- 沒有先后順序,是一種「完全異步執(zhí)行」的廣播。其他接收者幾乎同一時刻收到這條廣播消息,效率高,無法被截斷。
-
有序廣播
- 有先后順序,是一種「同步執(zhí)行」的廣播。同一時刻只有一個接收者可以收到這個廣播消息,優(yōu)先級高的廣播接收器可以先收到,且前面的廣播接收器還可以截斷正在傳遞的廣播,導致后面的廣播接收器就無法接收到廣播。
依據(jù)范圍方式分類
-
全局廣播
- 廣播可以被其他任意的應用程序接收,或者可接收來自其他任意應用程序的廣播
-
本地廣播
- 廣播只在應用程序的內部進行傳遞,接收器也只能接收內部廣播,不能接受其他應用程序的廣播
依據(jù)注冊方式分類
- 靜態(tài)廣播
不管應用程序是否處于活動狀態(tài),都會進行監(jiān)聽。每次觸發(fā)都會建立新的 Receiver 對象。
- 動態(tài)廣播
在代碼中進行注冊,注意動態(tài)注冊的廣播一定要取消注冊才行,通常是在 onDestroy() 方法中調用 unregisterReceiver() 方法來實現(xiàn)。
從開始創(chuàng)建直到其被解除注冊會使用同一個 Receiver,無論這個廣播被觸發(fā)幾次。
廣播使用
創(chuàng)建BroadcastReceiver
繼承 BroadcastReceiver,實現(xiàn)父類的 onReceive() 方法即可,如下示例:
public class MyReceiver extends BroadcastReceiver {
// 自定義 action
private static final String ACTION = "com.demo.broadcast.MyReceiverFilter";
@Override
public void onReceive(Context context, Intent intent) {
//TODO: 接收到廣播進行處理
}
}
注冊靜態(tài)廣播
注冊靜態(tài)廣播:在 AndroidMainfest 文件中配置
<receiver android:name=".broadcast.MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<!-- 接收系統(tǒng)開機廣播 -->
<action android:name="android.intent.action.BOOT_COMPLETED" />
<!-- 接收自定義廣播 -->
<action android:name="com.demo.broadcast.MyReceiverFilter" />
</intent-filter>
</receiver>
上面 enabled 設置為 true 表示能接收到廣播;exported 設置為 true 表示能接收到外部 APK 發(fā)送的廣播。
注冊動態(tài)廣播
動態(tài)廣播:只需在代碼中注冊即可
// 創(chuàng)建廣播
MyReceiver myReceiver = new MyReceiver();
// 創(chuàng)建 IntentFilter
IntentFilter intentFilter = new IntentFilter();
// 添加系統(tǒng)廣播 action :監(jiān)聽網(wǎng)絡變化
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
// 添加自定義 action
intentFilter.addAction(MyReceiver.ACTION);
// 注冊廣播
registerReceiver(myReceiver, intentFilter);
// 注銷廣播
unregisterReceiver(myReceiver);
發(fā)送廣播
靜態(tài)廣播和動態(tài)廣播發(fā)送都是如下處理方式(系統(tǒng)廣播是Android系統(tǒng)自動發(fā)送)
// 創(chuàng)建 Intent
Intent intent = new Intent();
// 添加action(可自定義)
intent.setAction(MyReceiver.ACTION);
// 發(fā)送廣播
sendBroadcast(intent);
帶權限的廣播
使用廣播可能引發(fā)的安全問題:
- 若第三方應用監(jiān)聽我們的廣播,則會造成我們應用的數(shù)據(jù)泄露;
- 若第三方應用冒充我方應用發(fā)送廣播,就會頻繁的啟動廣播接收程序,造成應用混亂,甚至崩潰。
為了避免以上安全問題,Android 提供了權限機制。
在注冊靜態(tài)廣播時,可在 AndroidMainfest 文件中添加權限
<manifest ...>
<!-- 自定義一個權限 -->
<permission android:name="com.demo.permissions.MY_BROADCAST"/>
<!-- 使用自定義的權限 -->
<uses-permission android:name="com.demo.permissions.MY_BROADCAST"/>
<application ...>
<!-- 添加權限 -->
<receiver android:name=".broadcast.MyReceiver"
android:permission="com.demo.broadcast.MY_BROADCAST"
android:enabled="true"
android:exported="true">
<intent-filter>
<!-- 接收自定義廣播 -->
<action android:name="com.demo.broadcast.MyReceiverFilter" />
</intent-filter>
</receiver>
</application>
</manifest>
在發(fā)送廣播時,可為其指定一個權限,只允許有該權限的應用才能接收到廣播,以下示例:
// 創(chuàng)建 Intent
Intent intent = new Intent();
// 添加自定義 action
intent.setAction(MyReceiver.ACTION);
// 發(fā)送廣播,添加權限
sendBroadcast(intent, "com.demo.permissions.MY_BROADCAST");