【翻译】(61)receiver元素

本文详细解析了Android应用中广播接收器的基本概念、语法、使用方式及其关键属性,包括如何声明、配置和注册广播接收器,以及如何通过权限、意图过滤器等控制其行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

【翻译】(61)receiver元素

 

see

http://developer.android.com/guide/topics/manifest/receiver-element.html

 

原文见

http://developer.android.com/guide/topics/manifest/receiver-element.html

 

-------------------------------

 

<receiver>

 

receiver元素

 

-------------------------------

 

* syntax:

 

* 语法:

 

-------------------------------

 

<receiver 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>

 

-------------------------------

 

* contained in:

 

* 被包含在:

 

<application>

 

* can contain:

 

* 可以包含:

 

<intent-filter> 

<meta-data>

 

* description:

 

* 描述:

 

Declares a broadcast receiver (a BroadcastReceiver subclass) as one of the application's components. Broadcast receivers enable applications to receive intents that are broadcast by the system or by other applications, even when other components of the application are not running.

 

声明一个广播接收器(一个BroadcastReceiver子类)作为应用程序组件之一。广播接收器使应用程序能接收由系统或其它应用程序广播的意图,即便当应用程序的其它组件不在运行。

 

There are two ways to make a broadcast receiver known to the system: One is declare it in the manifest file with this element. The other is to create the receiver dynamically in code and register it with the Context.registerReceiver() method. See the BroadcastReceiver class description for more on dynamically created receivers.

 

有两种方式使一个广播接收器让系统知道:一种方式是在清单文件中用这个元素声明它。其它方式是在代码中动态地创建接收器并且用Context.registerReceiver()方法注册它。见BroadcastReceiver类的描述以获得关于被动态创建的接收器的更多信息。

 

* attributes:

 

* 属性:

 

* android:enabled

 

Whether or not the broadcast receiver can be instantiated by the system — "true" if it can be, and "false" if not. The default value is "true".

 

广播接收器是否可以被系统实例化——"true"如果它可以,而"false"如果它不可以。默认值为"true"。

 

The <application> element has its own enabled attribute that applies to all application components, including broadcast receivers. The <application> and <receiver> attributes must both be "true" for the broadcast receiver to be enabled. If either is "false", it is disabled; it cannot be instantiated.

 

<application>元素拥有它自己的enabled属性,它应用到所有应用程序组件,包括广播接收器。<application>和<receiver>属性必须都是"true"以让广播接收器被使能。如果其中一个为"false",它被屏蔽;它不能被实例化。

 

* android:exported

 

Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID.

 

广播接收器是否可以从它的应用程序外部的源接收消息——"true"如果它可以,而"false"如果它不可以。如果"false",广播接收器可以接收的消息只是由相同应用程序或带有相同用户ID的应用程序的组件发出的那些消息。

 

The default value depends on whether the broadcast receiver contains intent filters. The absence of any filters means that it can be invoked only by Intent objects that specify its exact class name. This implies that the receiver is intended only for application-internal use (since others would not normally know the class name). So in this case, the default value is "false". On the other hand, the presence of at least one filter implies that the broadcast receiver is intended to receive intents broadcast by the system or other applications, so the default value is "true".

 

默认值依赖于广播接收器是否包含意图过滤器。不存在任何过滤器意味着它只可以被指定它的具体类名的Intent对象调用。这暗示接收器倾向于只给应用程序内部使用(因为其它应用程序通常不知道该类名)。所以在这种情况下,默认值为"false"。另一方面,存在至少一个过滤器暗示广播接收器倾向于接收由系统或其它应用程序广播的意图,所以默认值为"true"。

 

This attribute is not the only way to limit a broadcast receiver's external exposure. You can also use a permission to limit the external entities that can send it messages (see the permission attribute).

 

这个属性并非限制一个广播接收器的对外暴露的唯一方式。你也可以使用一个权限来限制可以向它发送消息的外部实体(见permission属性)。

 

* android:icon

 

An icon representing the broadcast receiver. This attribute must be set as a reference to a drawable resource containing the image definition. If it is not set, the icon specified for the application as a whole is used instead (see the <application> element's icon attribute).

 

一个代表广播接收器的图标。这个属性必须被设置为指向包含图片定义的可绘画对象资源的引用。如果它不被设置,那么改为使用为应用程序整体指定的图标(见<application>元素的icon属性)。

 

The broadcast receiver's icon — whether set here or by the <application> element — is also the default icon for all the receiver's intent filters (see the <intent-filter> element's icon attribute).

 

广播接收器的图标——不管在这里设置还是通过<application>元素——也是用于所有接收器的意图过滤器的默认图标(见<intent-filter>元素的icon属性)。

 

* android:label

 

A user-readable label for the broadcast receiver. If this attribute is not set, the label set for the application as a whole is used instead (see the <application> element's label attribute).

 

一个用于广播接收器的用户可读标签。如果此属性没有被设置,那么改为使用为应用程序整体设置的标签(见<application>元素的label属性)。

 

The broadcast receiver's label — whether set here or by the <application> element — is also the default label for all the receiver's intent filters (see the <intent-filter> element's label attribute).

 

广播接收器的标签——不管在这里设置还是通过<application>元素——也是用于所有接收器的意图过滤器的默认标签(见<intent-filter>元素的label属性)。

 

The label should be set as a reference to a string resource, so that it can be localized like other strings in the user interface. However, as a convenience while you're developing the application, it can also be set as a raw string.

 

标签应该被设置为一个指向字符串资源的引用,使它可以像用户界面中的其它字符串那样被本地化。然而,作为一种便利当你正在开发应用程序时,它还可以被设置为一个原始字符串。

 

* android:name

 

The name of the class that implements the broadcast receiver, a subclass of BroadcastReceiver. This should be a fully qualified class name (such as, "com.example.project.ReportReceiver"). However, as a shorthand, if the first character of the name is a period (for example, ". ReportReceiver"), it is appended to the package name specified in the <manifest> element.

 

实现广播接收器的类,一个BroadcastReceiver子类的名称。它应该是一个完全修饰类名(诸如,"com.example.project.ReportReceiver")。然而,作为简写,如果名称的第一个字符是一个点号(例如,". ReportReceiver"),那么它被尾加到<manifest>元素指定的包名后。

 

Once you publish your application, you should not change this name (unless you've set android:exported="false").

 

一旦你发布你的应用程序,你不应该改变这个名称(除非你已经设置了android:exported="false")。

 

There is no default. The name must be specified.

 

没有默认值。该名称必须被指定。

 

* android:permission

 

The name of a permission that broadcasters must have to send a message to the broadcast receiver. If this attribute is not set, the permission set by the <application> element's permission attribute applies to the broadcast receiver. If neither attribute is set, the receiver is not protected by a permission.

 

权限的名称,广播器必须拥有这个权限来发送消息到广播接收器。如果这个属性没有被设置,那么被<application>元素的permission属性设置的权限应用到广播接收器。如果两个回溯性都没有被设置,那么接收器不被一个权限保护。

 

For more information on permissions, see the Permissions section in the introduction and a separate document, Security and Permissions.

 

想获得关于权限的更多信息,请参见介绍中的权限章节以及单独的文档,安全与权限。

 

* android:process

 

The name of the process in which the broadcast receiver should run. Normally, all components of an application run in the default process created for the application. It has the same name as the application package. The <application> element's process attribute can set a different default for all components. But each component can override the default with its own process attribute, allowing you to spread your application across multiple processes.

 

广播接收器应该运行在的进程的名称。通常,一个应用程序的所有组件运行在为应用程序创建的默认进程中。它拥有与应用程序包相同的名称。<application>元素的process属性可以为所有组件设置一个不同的默认值。但每个组件可以用它自己的process属性覆盖该默认值,允许你跨多个进程延展你的应用程序。

 

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the broadcast receiver runs in that process. If the process name begins with a lowercase character, the receiver will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

 

如果赋给此属性的名称以一个冒号(':')开头,那么对于应用程序私有的一个新进程被创建,当需要它并且广播接收器运行在那个进程中时。如果进程名称以一个小写字符开头,那么接收器将运行在那个名称的全局进程中,假设它拥有权限这样做。它允许不同应用程序的组件共享一个进程,降低资源使用率。

 

* introduced in:

 

* 引入:

 

API Level 1

 

API级别1

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 14 Feb 2012 21:12

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)

 

(本人翻译质量欠佳,请以官方最新内容为准,或者参考其它翻译版本:

* ソフトウェア技術ドキュメントを勝手に翻訳

http://www.techdoctranslator.com/android

* Ley's Blog

http://leybreeze.com/blog/

* 农民伯伯

http://www.cnblogs.com/over140/

* Android中文翻译组

http://androidbox.sinaapp.com/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值