Android 應用程式會從 Android 系統和其他 Android 應用程式傳送及接收廣播訊息,類似於發布/訂閱設計模式。系統和應用程式通常會在發生特定事件時傳送廣播。舉例來說,Android 系統會在發生各種系統事件時傳送廣播,例如系統啟動或裝置充電。應用程式也會傳送自訂廣播訊息,例如通知其他應用程式可能感興趣的內容 (例如下載新資料)。
應用程式可以註冊接收特定廣播。傳送廣播訊息時,系統會自動將訊息傳送至已訂閱接收該特定類型廣播訊息的應用程式。
一般來說,廣播訊息可用於應用程式之間,以及正常使用者流程以外的通訊系統。不過,請務必謹慎,不要濫用回應廣播和在背景執行作業的機會,以免導致系統效能緩慢。
關於系統廣播訊息
發生各種系統事件時,系統會自動傳送廣播,例如系統切換飛航模式時。所有已訂閱的應用程式都會收到這些廣播。
Intent 物件會包裝廣播訊息。action 字串會指出發生的事件,例如 android.intent.action.AIRPLANE_MODE。意圖的額外欄位中可能也會包含其他資訊。舉例來說,「飛航模式」意圖包含布林值額外資訊,指出飛航模式是否開啟。
如要進一步瞭解如何讀取意圖及從意圖取得動作字串,請參閱「意圖和意圖篩選器」。
系統廣播動作
如需系統廣播訊息動作的完整清單,請參閱 Android SDK 中的 BROADCAST_ACTIONS.TXT
檔案。每個廣播動作都有相關聯的常數欄位。舉例來說,常數 ACTION_AIRPLANE_MODE_CHANGED 的值為 android.intent.action.AIRPLANE_MODE。每個廣播動作的說明文件都位於相關聯的常數欄位中。
系統廣播異動
隨著 Android 平台不斷演進,系統廣播的行為也會定期變更。請注意下列異動,以便支援所有 Android 版本。
Android 16
在 Android 16 中,使用 android:priority 屬性或 IntentFilter.setPriority() 跨不同程序傳送的廣播,無法保證傳送順序。廣播優先順序只會在相同應用程式程序內生效,不會影響所有程序。
此外,廣播優先順序會自動限制在 (SYSTEM_LOW_PRIORITY + 1, SYSTEM_HIGH_PRIORITY - 1) 範圍內。只有系統元件可以將 SYSTEM_LOW_PRIORITY、SYSTEM_HIGH_PRIORITY 設為廣播優先順序。
Android 14
當應用程式處於快取狀態時,系統會為確保系統運作正常,最佳化廣播訊息傳送作業。舉例來說,當應用程式處於快取狀態時,系統會延後處理重要性較低的系統廣播 (例如 ACTION_SCREEN_ON)。應用程式從快取狀態進入有效程序生命週期後,系統就會傳送所有延遲的廣播。
資訊清單中宣告的重要廣播訊息會暫時從快取狀態移除應用程式,以便傳送。
Android 9
自 Android 9 (API 級別 28) 開始,NETWORK_STATE_CHANGED_ACTION 廣播不會收到使用者位置資訊或個人識別資料。
如果應用程式安裝在搭載 Android 9.0 (API 級別 28) 以上版本的裝置上,系統不會在 Wi-Fi 廣播中加入 SSID、BSSID、連線資訊或掃描結果。如要取得這項資訊,請改為呼叫 getConnectionInfo()。
Android 8.0
從 Android 8.0 (API 級別 26) 開始,系統會對資訊清單宣告的接收器施加額外限制。
如果應用程式指定 Android 8.0 以上版本,就無法使用資訊清單宣告大多數隱含廣播 (並非專為應用程式而設的廣播) 的接收器。使用者積極使用應用程式時,您仍可使用內容註冊的接收器。
Android 7.0
Android 7.0 (API 級別 24) 以上版本不會傳送下列系統廣播:
此外,指定 Android 7.0 以上版本的應用程式必須使用 registerReceiver(BroadcastReceiver, IntentFilter) 註冊 CONNECTIVITY_ACTION 廣播。在資訊清單中宣告接收器無效。
接收廣播
應用程式可以透過兩種方式接收廣播:透過內容註冊的接收器和資訊清單宣告的接收器。
註冊使用情境的接收器
只要註冊接收器的情境有效,接收器就會收到廣播訊息。這通常介於 registerReceiver 和 unregisterReceiver 的呼叫之間。系統刪除對應的情境時,註冊情境也會失效。舉例來說,如果您在 Activity 環境中註冊,只要活動保持啟用狀態,您就會收到廣播。如果您使用應用程式情境註冊,只要應用程式正在執行,您就會收到廣播訊息。
如要向內容註冊接收器,請按照下列步驟操作:
在應用程式的模組層級建構檔案中,加入 AndroidX Core 程式庫 1.9.0 以上版本:
Groovy
dependencies { def core_version = "1.18.0" // Java language implementation implementation "androidx.core:core:$core_version" // Kotlin implementation "androidx.core:core-ktx:$core_version" // To use RoleManagerCompat implementation "androidx.core:core-role:1.1.0" // To use the Animator APIs implementation "androidx.core:core-animation:1.0.0" // To test the Animator APIs androidTestImplementation "androidx.core:core-animation-testing:1.0.0" // Optional - To enable APIs that query the performance characteristics of GMS devices. implementation "androidx.core:core-performance:1.0.0" // Optional - to use ShortcutManagerCompat to donate shortcuts to be used by Google implementation "androidx.core:core-google-shortcuts:1.1.0" // Optional - to support backwards compatibility of RemoteViews implementation "androidx.core:core-remoteviews:1.1.0" // Optional - APIs for SplashScreen, including compatibility helpers on devices prior Android 12 implementation "androidx.core:core-splashscreen:1.2.0" }
Kotlin
dependencies { val core_version = "1.18.0" // Java language implementation implementation("androidx.core:core:$core_version") // Kotlin implementation("androidx.core:core-ktx:$core_version") // To use RoleManagerCompat implementation("androidx.core:core-role:1.1.0") // To use the Animator APIs implementation("androidx.core:core-animation:1.0.0") // To test the Animator APIs androidTestImplementation("androidx.core:core-animation-testing:1.0.0") // Optional - To enable APIs that query the performance characteristics of GMS devices. implementation("androidx.core:core-performance:1.0.0") // Optional - to use ShortcutManagerCompat to donate shortcuts to be used by Google implementation("androidx.core:core-google-shortcuts:1.1.0") // Optional - to support backwards compatibility of RemoteViews implementation("androidx.core:core-remoteviews:1.1.0") // Optional - APIs for SplashScreen, including compatibility helpers on devices prior Android 12 implementation("androidx.core:core-splashscreen:1.2.0") }
建立
BroadcastReceiver的執行個體:Kotlin
val myBroadcastReceiver = MyBroadcastReceiver()Java
MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();建立
IntentFilter的執行個體:Kotlin
val filter = IntentFilter("com.example.snippets.ACTION_UPDATE_DATA")Java
IntentFilter filter = new IntentFilter("com.example.snippets.ACTION_UPDATE_DATA");選擇是否要匯出廣播接收器,並向裝置上的其他應用程式顯示。如果這個接收器正在監聽系統或其他應用程式 (包括您擁有的其他應用程式) 傳送的廣播,請使用
RECEIVER_EXPORTED標記。如果接收器只監聽應用程式傳送的廣播,請使用RECEIVER_NOT_EXPORTED旗標。Kotlin
val listenToBroadcastsFromOtherApps = false val receiverFlags = if (listenToBroadcastsFromOtherApps) { ContextCompat.RECEIVER_EXPORTED } else { ContextCompat.RECEIVER_NOT_EXPORTED }Java
boolean listenToBroadcastsFromOtherApps = false; int receiverFlags = listenToBroadcastsFromOtherApps ? ContextCompat.RECEIVER_EXPORTED : ContextCompat.RECEIVER_NOT_EXPORTED;呼叫
registerReceiver()註冊接收器:Kotlin
ContextCompat.registerReceiver(context, myBroadcastReceiver, filter, receiverFlags)Java
ContextCompat.registerReceiver(context, myBroadcastReceiver, filter, receiverFlags);如要停止接收廣播,請呼叫
unregisterReceiver(android.content.BroadcastReceiver)。不再需要接收器或內容失效時,請務必取消註冊接收器。
取消註冊廣播接收器
註冊廣播接收器時,接收器會保留您註冊時使用的 Context 參照。如果接收端的註冊範圍超出 Context 生命週期範圍,就可能導致外洩。舉例來說,如果您在 Activity 範圍內註冊接收器,但忘記在系統刪除 Activity 時取消註冊,就可能發生這種情況。因此,請務必取消註冊廣播接收器。
Kotlin
class MyActivity : ComponentActivity() {
private val myBroadcastReceiver = MyBroadcastReceiver()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// ...
ContextCompat.registerReceiver(this, myBroadcastReceiver, filter, receiverFlags)
setContent { MyApp() }
}
override fun onDestroy() {
super.onDestroy()
// When you forget to unregister your receiver here, you're causing a leak!
this.unregisterReceiver(myBroadcastReceiver)
}
}
Java
class MyActivity extends ComponentActivity {
MyBroadcastReceiver myBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
ContextCompat.registerReceiver(this, myBroadcastReceiver, filter, receiverFlags);
// Set content
}
}
在最小範圍內註冊接收器
只有在您實際對結果感興趣時,才應註冊廣播接收器。選擇盡可能小的接收器範圍:
LifecycleResumeEffect或活動onResume/onPause生命週期方法:只有在應用程式處於已恢復狀態時,廣播接收器才會收到更新。LifecycleStartEffect或活動onStart/onStop生命週期方法:只有在應用程式處於已恢復狀態時,廣播接收器才會收到更新。DisposableEffect:只有在可組合函式位於組合樹狀結構中時,廣播接收器才會收到更新。這個範圍未附加至活動生命週期範圍。建議您在應用程式環境中註冊接收器。這是因為可組合函式理論上可能會比活動生命週期範圍更長壽,並導致活動洩漏。- 活動
onCreate/onDestroy:廣播接收器會在活動處於建立狀態時接收更新。請務必在onDestroy()中取消註冊,而非onSaveInstanceState(Bundle),因為系統可能不會呼叫onSaveInstanceState(Bundle)。 - 自訂範圍:舉例來說,您可以在
ViewModel範圍中註冊接收器,這樣接收器就能在活動重建後繼續運作。請務必使用應用程式內容註冊接收器,因為接收器可能會超出活動生命週期範圍,並洩漏活動。
建立有狀態和無狀態的可組合函式
Compose 具有有狀態和無狀態的可組合函式。在可組合函式內註冊或取消註冊廣播接收器,會使其成為有狀態的函式。可組合函式並非決定性函式,傳遞相同參數時不會算繪相同內容。內部狀態可能會根據對已註冊的廣播接收器的呼叫而變更。
在 Compose 中,我們建議的最佳做法是將可組合函式分成有狀態和無狀態版本。因此,建議您將廣播接收器的建立作業從 Composable 中提升,使其成為無狀態:
@Composable
fun MyStatefulScreen() {
val myBroadcastReceiver = remember { MyBroadcastReceiver() }
val context = LocalContext.current
LifecycleStartEffect(true) {
// ...
ContextCompat.registerReceiver(context, myBroadcastReceiver, filter, flags)
onStopOrDispose { context.unregisterReceiver(myBroadcastReceiver) }
}
MyStatelessScreen()
}
@Composable
fun MyStatelessScreen() {
// Implement your screen
}
資訊清單宣告的接收器
如果您在資訊清單中宣告廣播接收器,系統會在傳送廣播時啟動應用程式。如果應用程式尚未執行,系統會啟動該應用程式。
如要在資訊清單中宣告廣播接收器,請執行下列步驟:
在應用程式資訊清單中指定
<receiver>元素。<!-- If this receiver listens for broadcasts sent from the system or from other apps, even other apps that you own, set android:exported to "true". --> <receiver android:name=".MyBroadcastReceiver" android:exported="false"> <intent-filter> <action android:name="com.example.snippets.ACTION_UPDATE_DATA" /> </intent-filter> </receiver>意圖篩選器會指定接收器訂閱的廣播動作。
子類別
BroadcastReceiver並實作onReceive(Context, Intent)。在下列範例中,廣播接收器會記錄並顯示廣播內容:Kotlin
class MyBroadcastReceiver : BroadcastReceiver() { @Inject lateinit var dataRepository: DataRepository override fun onReceive(context: Context, intent: Intent) { if (intent.action == "com.example.snippets.ACTION_UPDATE_DATA") { val data = intent.getStringExtra("com.example.snippets.DATA") ?: "No data" // Do something with the data, for example send it to a data repository: dataRepository.updateData(data) } } }Java
public static class MyBroadcastReceiver extends BroadcastReceiver { @Inject DataRepository dataRepository; @Override public void onReceive(Context context, Intent intent) { if (Objects.equals(intent.getAction(), "com.example.snippets.ACTION_UPDATE_DATA")) { String data = intent.getStringExtra("com.example.snippets.DATA"); // Do something with the data, for example send it to a data repository: if (data != null) { dataRepository.updateData(data); } } } }
系統套件管理員會在安裝應用程式時註冊接收器。接收器隨後會成為應用程式的獨立進入點,也就是說,如果應用程式未執行,系統可以啟動應用程式並傳送廣播。
系統會建立新的 BroadcastReceiver 元件物件,處理收到的每項廣播。這個物件僅在呼叫 onReceive(Context, Intent) 的期間有效。程式碼從這個方法傳回後,系統會將元件視為不再處於活動狀態。
對程序狀態的影響
BroadcastReceiver 是否運作會影響其中包含的程序,進而改變系統終止程序的可能性。前景程序會執行接收器的 onReceive() 方法。系統會執行該程序,除非記憶體壓力極大。
系統會在 onReceive() 後停用 BroadcastReceiver。
接收端的主機程序重要性取決於應用程式元件。如果該程序只代管資訊清單宣告的接收器,系統可能會在 onReceive() 後終止該程序,以便為其他更重要的程序釋出資源。使用者從未或最近未與應用程式互動時,通常會發生這種情況。
因此,廣播接收器不應啟動長時間執行的背景執行緒。系統隨時可以在 onReceive() 後停止程序,以回收記憶體並終止建立的執行緒。如要讓程序保持運作,請使用 JobScheduler 從接收器排程 JobService,讓系統知道程序仍在運作。詳情請參閱「背景工作總覽」。
傳送廣播
Android 提供兩種應用程式傳送廣播的方式:
sendOrderedBroadcast(Intent, String)方法一次會將廣播訊息傳送給一個接收器。由於每個接收器會按順序執行,因此可將結果傳播至下一個接收器。也可以完全中止廣播,避免傳送至其他接收器。您可以控制接收器在同一應用程式程序中執行的順序。如要這麼做,請使用相符意圖篩選器的android:priority屬性。如果接收器的優先順序相同,系統會任意決定執行順序。sendBroadcast(Intent)方法會以未定義的順序,將廣播傳送至所有接收器。這稱為「一般廣播」。這樣效率較高,但接收器無法讀取其他接收器的結果、傳播從廣播接收的資料,或取消播送。
下列程式碼片段說明如何建立 Intent 並呼叫 sendBroadcast(Intent),藉此傳送廣播。
Kotlin
val intent = Intent("com.example.snippets.ACTION_UPDATE_DATA").apply {
putExtra("com.example.snippets.DATA", newData)
setPackage("com.example.snippets")
}
context.sendBroadcast(intent)
Java
Intent intent = new Intent("com.example.snippets.ACTION_UPDATE_DATA");
intent.putExtra("com.example.snippets.DATA", newData);
intent.setPackage("com.example.snippets");
context.sendBroadcast(intent);
廣播訊息會包裝在 Intent 物件中。意圖的 action字串必須提供應用程式的 Java 套件名稱語法,並明確識別廣播事件。你可以使用 putExtra(String, Bundle) 將額外資訊附加至意圖。您也可以在意圖上呼叫 setPackage(String),將廣播限制為同一機構中的一組應用程式。
使用權限限制廣播
您可以透過權限,將廣播限制為一組具有特定權限的應用程式。您可以對廣播的傳送者或接收者強制執行限制。
傳送廣播 (須具備權限)
呼叫 sendBroadcast(Intent, String) 或 sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String,
Bundle) 時,您可以指定權限參數。只有在資訊清單中透過 <uses-permission> 標記要求該權限的接收器,才能接收廣播。如果權限屬於危險權限,您必須先授予權限,接收器才能接收廣播。舉例來說,下列程式碼會傳送具有權限的廣播:
Kotlin
context.sendBroadcast(intent, android.Manifest.permission.ACCESS_COARSE_LOCATION)
Java
context.sendBroadcast(intent, android.Manifest.permission.ACCESS_COARSE_LOCATION);
如要接收廣播,接收端應用程式必須要求下列權限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
您可以指定現有的系統權限,例如 BLUETOOTH_CONNECT,也可以使用 <permission> 元素定義自訂權限。如要瞭解一般權限和安全性,請參閱「系統權限」。
接收具有權限的廣播
如果您在註冊廣播接收器時指定權限參數 (使用 registerReceiver(BroadcastReceiver, IntentFilter, String, Handler) 或資訊清單中的 <receiver> 標記),則只有在資訊清單中以 <uses-permission> 標記要求權限的廣播器,才能將 Intent 傳送至接收器。如果權限屬於危險等級,播送者也必須取得該權限。
舉例來說,假設接收端應用程式的資訊清單宣告接收器如下:
<!-- If this receiver listens for broadcasts sent from the system or from
other apps, even other apps that you own, set android:exported to "true". -->
<receiver
android:name=".MyBroadcastReceiverWithPermission"
android:permission="android.permission.ACCESS_COARSE_LOCATION"
android:exported="true">
<intent-filter>
<action android:name="com.example.snippets.ACTION_UPDATE_DATA" />
</intent-filter>
</receiver>
或者,接收端應用程式具有已註冊使用情境的接收器,如下所示:
Kotlin
ContextCompat.registerReceiver(
context, myBroadcastReceiver, filter,
android.Manifest.permission.ACCESS_COARSE_LOCATION,
null, // scheduler that defines thread, null means run on main thread
receiverFlags
)
Java
ContextCompat.registerReceiver(
context, myBroadcastReceiver, filter,
android.Manifest.permission.ACCESS_COARSE_LOCATION,
null, // scheduler that defines thread, null means run on main thread
receiverFlags
);
接著,如要將廣播傳送給這些接收者,傳送應用程式必須要求下列權限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
安全性考量
以下是傳送及接收廣播時的一些安全性考量:
如果許多應用程式已註冊在資訊清單中接收相同的廣播,系統可能會啟動大量應用程式,對裝置效能和使用者體驗造成重大影響。為避免這種情況,建議使用內容註冊,而非資訊清單宣告。有時 Android 系統本身會強制使用已註冊使用情境的接收器。舉例來說,
CONNECTIVITY_ACTION廣播只會傳送給已註冊環境的接收器。請勿使用隱含意圖播送私密資訊。只要註冊接收廣播,任何應用程式都能讀取這項資訊。你可以透過三種方式控管誰能接收廣播:
- 傳送廣播時,您可以指定權限。
- 在 Android 4.0 (API 級別 14) 以上版本中,傳送廣播時可以透過
setPackage(String)指定套件。系統會將廣播限制在與套件相符的應用程式集。
註冊接收器後,任何應用程式都可能將惡意廣播訊息傳送至應用程式的接收器。您可以透過下列幾種方式,限制應用程式接收的廣播:
- 註冊廣播接收器時,您可以指定權限。
- 如果是資訊清單宣告的接收器,您可以在資訊清單中將 android:exported 屬性設為「false」。接收器不會接收來自應用程式外部來源的廣播。
廣播動作的命名空間是全域命名空間。請確認動作名稱和其他字串是寫在您擁有的命名空間中。否則可能會不慎與其他應用程式發生衝突。
由於接收器的
onReceive(Context, Intent)方法會在主執行緒上執行,因此應快速執行並傳回。如果需要執行長時間的工作,請謹慎產生執行緒或啟動背景服務,因為系統可能會在onReceive()傳回後終止整個程序。詳情請參閱「對程序狀態的影響」。如要執行長時間執行的工作,建議您:- 在接收器的
onReceive()方法中呼叫goAsync(),並將BroadcastReceiver.PendingResult傳遞至背景執行緒。這樣一來,從onReceive()返回後,廣播就會繼續進行。不過,即使採用這種方法,系統仍會預期您在很短的時間內 (10 秒內) 結束廣播。但可讓您將工作移至其他執行緒,避免主執行緒發生故障。 - 使用
JobScheduler安排工作。詳情請參閱「智慧型工作排程」。
- 在接收器的
請勿從廣播接收器啟動活動,因為這樣會造成使用者體驗不佳,尤其是當接收器超過一個時。建議改為顯示通知。