- 語法:
<receiver android:directBootAware=["true" | "false"] android:enabled=["true" | "false"] android:exported=["true" | "false"] android:icon="drawable resource" android:label="string resource" android:name="string" android:permission="string" android:process="string" > ... </receiver>
- 包含於:
<application>
- 可包含:
<intent-filter>
<meta-data>
- 說明:
- 將廣播接收器 (
BroadcastReceiver
子類別) 宣告為應用程式的元件之一。廣播接收器可讓應用程式接收由系統或其他應用程式廣播的意圖,即使應用程式的其他元件並未執行也沒問題。您可以透過兩種方式讓系統得知廣播接收器。一種是使用此元素在資訊清單檔案中進行宣告,另一種則是在程式碼中動態建立接收器,並使用
Context.registerReceiver()
方法或其中一個超載版本註冊該接收器。如要進一步瞭解如何動態建立接收器,請參閱
BroadcastReceiver
類別說明。如果這個接收器處理了非系統廣播訊息,請指定
android:exported
的值。如果您希望接收器能夠接收其他應用程式的廣播訊息,請將這個值設為"true"
;若只想接收您自己應用程式的廣播訊息,請設為"false"
。如果您已宣告
android:permission
屬性,則無須將其移除。警告:請限制您在應用程式內設定的廣播接收器數量。如果廣播接收器過多,可能會影響應用程式效能以及使用者裝置的電池續航力。如要進一步瞭解您可以使用的 API,而非排定背景工作的
BroadcastReceiver
類別,請參閱「背景最佳化」。 - 屬性:
android:directBootAware
指出廣播接收器是否具有「直接啟動感知特性」,即是否可在使用者解鎖裝置前執行。
注意:在直接啟動期間,應用程式中的廣播接收器只能存取儲存在「受裝置保護」儲存空間中的資料。
預設值為
"false"
。android:enabled
- 指出廣播接收器是否可由系統例項化。
"true"
表示可以,"false"
表示不可以。預設值為"true"
。<application>
元素有專屬的enabled
屬性,適用於包括廣播接收器在內的所有應用程式元件。<application>
和<receiver>
屬性都必須是"true"
,才能啟用廣播接收器。如有任一屬性為"false"
,系統會停用廣播接收器,且無法將其例項化。 android:exported
- 指出廣播接收器是否可以接收其應用程式以外的非系統源訊息。
"true"
表示可以,"false"
表示不可以。如為"false"
,廣播接收器只會接收由系統、相同應用程式的元件,或是含有相同使用者 ID 的應用程式傳送的訊息。如果未指定,預設值取決於廣播接收器是否包含意圖篩選器。如果接收方至少包含一個意圖篩選器,則預設值為
"true"
。否則,預設值為"false"
。這個屬性不是限制廣播接收器對外公開的唯一方法。您也可以使用權限來限制可向其傳送訊息的外部實體。詳情請參閱
permission
屬性。 android:icon
- 代表廣播接收器的圖示。這項屬性會設為包含圖片定義的可繪製資源參照。如未設定,系統會改用針對應用程式整體指定的圖示。詳情請參閱
<application>
元素的icon
屬性。廣播接收器的圖示 (無論是在這裡設定,或由
<application>
元素設定) 也是所有接收器的意圖篩選器的預設圖示。請參閱<intent-filter>
元素的icon
屬性。 android:label
- 使用者能夠理解的廣播接收器標籤。如未設定這項屬性,系統會改用針對應用程式整體設定的標籤。詳情請參閱
<application>
元素的label
屬性。廣播接收器的標籤 (無論是在這裡設定,或由
<application>
元素設定) 也是所有接收器的意圖篩選器的預設標籤。請參閱<intent-filter>
元素的label
屬性。這個標籤應設為字串資源的參照,這樣才能像使用者介面中的其他字串一樣進行本地化。不過為了方便起見,您也可以在開發應用程式時將其設為原始字串。
android:name
- 實作廣播接收器的類別名稱,這是
BroadcastReceiver
的子類別。此為完整的類別名稱,例如"com.example.project.ReportReceiver"
。不過為了精簡起見,如果名稱的第一個字元是半形句號,例如".ReportReceiver"
,則會加到<manifest>
元素中所指定套件名稱的後方。發布應用程式後,除非您已設定
android:exported="false"
,否則請勿變更這個名稱。這項屬性沒有預設值。必須指定名稱。
android:permission
- 播送者向廣播接收器傳送訊息所需權限的名稱。如未設定這個屬性,由
<application>
元素的permission
屬性所設定的權限會套用至該廣播接收器。如果未設定這兩個屬性,接收器就不會受到權限保護。 android:process
- 指定廣播接收器要在哪個名稱的程序中執行。一般來說,應用程式的所有元件都是在為應用程式建立的預設程序中執行。這個程序的名稱與應用程式套件的名稱相同。
<application>
元素的process
屬性可以為所有元件設定不同的預設值。不過,每個元件都可以使用本身的process
屬性覆寫預設值,讓您將應用程式分散至多個程序。如果指派給此屬性的名稱以半形冒號開頭 (
:
),系統會視需要建立一個該應用程式專屬的新程序,而廣播接收器會在該程序中執行。如果程序名稱的開頭是小寫英文字元,接收器會在採用該名稱的通用程序中執行,前提是該程序具備相關權限,這樣可讓不同應用程式中的元件共用程序,進而減少資源用量。
- 導入版本:
- API 級別 1
<接收者>
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# <receiver\u003e\n\nsyntax:\n:\n\n ```xml\n \u003creceiver android:directBootAware=[\"true\" | \"false\"]\n android:enabled=[\"true\" | \"false\"]\n android:exported=[\"true\" | \"false\"]\n android:icon=\"drawable resource\"\n android:label=\"string resource\"\n android:name=\"string\"\n android:permission=\"string\"\n android:process=\"string\" \u003e\n ...\n \u003c/receiver\u003e\n ```\n\ncontained in:\n: [\u003capplication\u003e](/guide/topics/manifest/application-element)\n\ncan contain:\n: [\u003cintent-filter\u003e](/guide/topics/manifest/intent-filter-element)\n\n [\u003cmeta-data\u003e](/guide/topics/manifest/meta-data-element)\n\ndescription:\n: Declares a broadcast receiver, a [BroadcastReceiver](/reference/android/content/BroadcastReceiver)\n subclass, as one of the application's components. Broadcast receivers enable\n applications to receive intents that are broadcast by the system or by other\n applications, even when other components of the application aren't running.\n\n\n There are two ways to make a broadcast receiver known to the system. One is to\n declare it in the manifest file with this element. The other is to create\n the receiver dynamically in code and register it with the\n [Context.registerReceiver()](/reference/android/content/Context#registerReceiver(android.content.BroadcastReceiver,%20android.content.IntentFilter))\n method or one of its overloaded versions.\n\n\n For more information about how to dynamically create receivers, see the\n [BroadcastReceiver](/reference/android/content/BroadcastReceiver) class\n description.\n\n\n If this receiver handles non-system broadcasts, specify a value for `android:exported`.\n Set this value to `\"true\"` if you want your receiver to be able to\n receiver broadcasts from other applications or `\"false\"` if you only\n want your receiver to be able to receive broadcasts from your own app.\n\n\n You don't have to remove the `android:permission` attribute if you\n already declared it.\n\n\n **Warning:** Limit how many broadcast\n receivers you set in your app. Having too many broadcast receivers can\n affect your app's performance and the battery life of users' devices.\n For more information about APIs you can use instead of the\n `BroadcastReceiver` class for scheduling background work, see\n [Background optimization](/topic/performance/background-optimization).\n\nattributes:\n:\n\n `android:directBootAware`\n\n : Whether the broadcast receiver is *Direct-Boot aware*, that\n is, whether it can run before the user unlocks the device.\n\n **Note:** During\n [Direct Boot](/training/articles/direct-boot), a broadcast\n receiver in your application can only access the data that is stored in\n *device protected* storage.\n\n The default value is `\"false\"`.\n\n `android:enabled`\n : Whether the broadcast receiver can be instantiated by the system. It's\n `\"true\"` if it can be, and `\"false\"` if not. The default value\n is `\"true\"`.\n\n\n The [\u003capplication\u003e](/guide/topics/manifest/application-element) element has its own\n [enabled](/guide/topics/manifest/application-element#enabled) attribute that applies to all\n application components, including broadcast receivers. The\n `\u003capplication\u003e` and\n `\u003creceiver\u003e` attributes must both be `\"true\"` for\n the broadcast receiver to be enabled. If either is `\"false\"`, it's\n disabled and can't be instantiated.\n\n `android:exported`\n : Whether the broadcast receiver can receive messages from non-system sources\n outside its application. It's `\"true\"` if it can, and `\"false\"`\n if not. If `\"false\"`, the only messages the broadcast receiver\n receives are those sent by the system, components of the same application, or applications\n with the same user ID.\n\n\n If unspecified, the default value depends on whether the broadcast receiver contains intent\n filters. If the receiver contains at least one intent filter, then the default value is\n `\"true\"`. Otherwise, the default value is `\"false\"`.\n\n\n This attribute is not the only way to limit a broadcast receiver's external exposure.\n You can also use a permission to limit the external entities that can send it messages.\n See the [permission](/guide/topics/manifest/receiver-element#prmsn) attribute.\n\n `android:icon`\n : An icon representing the broadcast receiver. This attribute is set\n as a reference to a drawable resource containing the image definition.\n If it isn't set, the icon specified for the application as a whole is used\n instead. See the [\u003capplication\u003e](/guide/topics/manifest/application-element)\n element's [icon](/guide/topics/manifest/application-element#icon) attribute.\n\n\n The broadcast receiver's icon, whether set here or by the\n `\u003capplication\u003e` element, is also the\n default icon for all the receiver's intent filters. See the\n [\u003cintent-filter\u003e](/guide/topics/manifest/intent-filter-element) element's\n [icon](/guide/topics/manifest/intent-filter-element#icon) attribute.\n\n `android:label`\n : A user-readable label for the broadcast receiver. If this attribute isn't\n set, the label set for the application as a whole is\n used instead. See the [\u003capplication\u003e](/guide/topics/manifest/application-element) element's\n [label](/guide/topics/manifest/application-element#label) attribute.\n\n\n The broadcast receiver's label, whether set here or by the\n `\u003capplication\u003e` element, is also the\n default label for all the receiver's intent filters. See the\n [\u003cintent-filter\u003e](/guide/topics/manifest/intent-filter-element) element's\n [label](/guide/topics/manifest/intent-filter-element#label) attribute.\n\n\n The label is set as a reference to a string resource, so that\n it can be localized like other strings in the user interface.\n However, as a convenience while you're developing the application,\n it can also be set as a raw string.\n\n `android:name`\n : The name of the class that implements the broadcast receiver, a subclass of\n [BroadcastReceiver](/reference/android/content/BroadcastReceiver). This is a fully qualified\n class name, such as `\"com.example.project.ReportReceiver\"`. However,\n as a shorthand, if the first character of the name is a period, for example,\n `\".ReportReceiver\"`, it is appended to the package name specified in\n the [\u003cmanifest\u003e](/guide/topics/manifest/manifest-element) element.\n\n Once you publish your application, [don't\n change this name](http://android-developers.blogspot.com/2011/06/things-that-cannot-change.html), unless you set [android:exported](#exported)`=\"false\"`.\n\n\n There is no default. The name must be specified.\n\n `android:permission`\n : The name of a permission that broadcasters need in order to send a\n message to the broadcast receiver.\n If this attribute isn't set, the permission set by the\n [\u003capplication\u003e](/guide/topics/manifest/application-element) element's\n [permission](/guide/topics/manifest/application-element#prmsn) attribute applies\n to the broadcast receiver. If neither attribute is set, the receiver\n isn't protected by a permission.\n\n\n For more information about permissions, see the\n [Permissions](/guide/topics/manifest/manifest-intro#perms)\n section in the app manifest overview and\n [Security tips](/guide/topics/security/security).\n\n `android:process`\n\n : The name of the process in which the broadcast receiver runs. Normally, all components of an application run in the default process created for the application. It has the same name as the application package.\u003cbr /\u003e\n\n The\n [\u003capplication\u003e](/guide/topics/manifest/application-element) element's\n [process](/guide/topics/manifest/application-element#proc) attribute can set a different\n default for all components. But each component can override the default\n with its own `process` attribute, letting you spread your\n application across multiple processes.\n\n\n If the name assigned to this attribute begins with a colon (`:`), a new\n process, private to the application, is created when it's needed, and\n the broadcast receiver runs in that process.\n\n\n If the process name begins with a lowercase character, the receiver runs\n in a global process of that name, provided that it has permission to do so.\n This lets components in different applications share a process, reducing\n resource usage.\n\nintroduced in:\n: API level 1"]]