- 语法:
<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
子类)声明为应用的组件之一。广播接收器允许应用接收由系统或其他应用广播的 intent,即使应用的其他组件并没有运行也是如此。让系统知道广播接收器有两种方法:一种是使用此元素在清单文件中声明广播接收器;另一种是在代码中动态创建接收器,并使用
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 的应用的组件发送的消息。如果未指定值,默认值取决于广播接收器是否包含 intent 过滤器。如果接收器至少包含一个 intent 过滤器,则默认值为
"true"
。否则,默认值为"false"
。此属性不是限制广播接收器对外公开的唯一方式。您还可以使用权限来限制可向其发送消息的外部实体。请参阅
permission
属性。 android:icon
- 表示广播接收器的图标。此属性应设置为对包含图片定义的可绘制资源的引用。如果未设置此属性,则改用为整个应用指定的图标。请参阅
<application>
元素的icon
属性。广播接收器的图标(无论是在此处设置还是由
<application>
元素设置)也是接收器的所有 intent 过滤器的默认图标。请参阅<intent-filter>
元素的icon
属性。 android:label
- 广播接收器的用户可读标签。如果未设置此属性,则改用整个应用的标签集。请参阅
<application>
元素的label
属性。广播接收器的标签(无论是在此处设置还是由
<application>
元素设置)也是接收器的所有 intent 过滤器的默认标签。请参阅<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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):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"]]