- cú pháp:
<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>
- có trong:
<application>
- có thể chứa:
<intent-filter>
<meta-data>
- mô tả:
- Khai báo một broadcast receiver (lớp con của
BroadcastReceiver
) làm một trong các thành phần của ứng dụng. Broadcast receiver giúp ứng dụng nhận được ý định do hệ thống hoặc ứng dụng khác truyền đi, ngay cả khi các thành phần khác của ứng dụng không chạy.Có 2 cách để hệ thống nhận biết được broadcast receiver. Một là khai báo trong tệp kê khai bằng phần tử này. Hai là tạo receiver một cách linh động ở dạng mã và đăng ký bằng phương thức
Context.registerReceiver()
hoặc một trong các phiên bản quá tải.Để biết thêm thông tin về cách tạo receiver một cách linh động, hãy xem phần mô tả của lớp
BroadcastReceiver
.Nếu receiver này xử lý các tin không phải do hệ thống truyền ra, hãy chỉ định một giá trị cho
android:exported
. Đặt giá trị này thành"true"
nếu bạn muốn receiver nhận tin từ các ứng dụng khác hoặc là"false"
nếu bạn chỉ muốn receiver nhận tin từ chính ứng dụng của bạn.Bạn không cần xoá thuộc tính
android:permission
nếu đã khai báo thuộc tính này.Cảnh báo: Hãy giới hạn số lượng broadcast receiver bạn đặt trong ứng dụng của mình. Việc có quá nhiều broadcast receiver có thể ảnh hưởng đến hiệu suất của ứng dụng cũng như thời lượng pin của thiết bị của người dùng. Để biết thêm thông tin về API mà bạn có thể sử dụng thay cho lớp
BroadcastReceiver
nhằm lập lịch biểu cho công việc ở chế độ nền, hãy xem bài viết Tối ưu hoá ở chế độ nền. - thuộc tính:
android:directBootAware
Liệu broadcast receiver có nhận biết được quá trình Khởi động trực tiếp (Direct-Boot) hay không, tức là broadcast receiver có thể chạy trước khi người dùng mở khoá thiết bị không.
Lưu ý: Trong quá trình Khởi động trực tiếp, broadcast receiver trong ứng dụng của bạn chỉ có thể truy cập dữ liệu được lưu trữ trong bộ nhớ được bảo vệ của thiết bị.
Giá trị mặc định là
"false"
.android:enabled
- Liệu hệ thống có thể tạo thực thể cho broadcast receiver này hay không. Giá trị này sẽ là
"true"
nếu hệ thống có thể và là"false"
nếu hệ thống không thể. Giá trị mặc định là"true"
.Phần tử
<application>
có thuộc tínhenabled
riêng áp dụng cho tất cả thành phần của ứng dụng, bao gồm cả broadcast receiver. Cả hai thuộc tính<application>
và<receiver>
phải là"true"
thì mới có thể bật broadcast receiver. Nếu là"false"
, broadcast receiver sẽ bị tắt và không thể tạo thực thể. android:exported
- Liệu broadcast receiver có thể nhận thông báo từ các nguồn không phải hệ thống ở bên ngoài ứng dụng hay không. Giá trị này sẽ là
"true"
nếu có thể và"false"
nếu không thể. Nếu là"false"
, thì thông báo duy nhất mà broadcast receiver nhận được là các thông báo do hệ thống, các thành phần của cùng một ứng dụng hoặc các ứng dụng có cùng mã nhận dạng người dùng gửi.Nếu không chỉ định, thì giá trị mặc định sẽ tuỳ thuộc vào việc broadcast receiver có chứa bộ lọc ý định hay không. Nếu broadcast receiver chứa ít nhất một bộ lọc ý định, thì giá trị mặc định sẽ là
"true"
. Nếu không, giá trị mặc định sẽ là"false"
.Thuộc tính này không phải là cách duy nhất để hạn chế việc tiếp xúc bên ngoài của broadcast receiver. Bạn cũng có thể sử dụng quyền để giới hạn các thực thể bên ngoài có thể gửi thông báo cho broadcast receiver. Xem thuộc tính
permission
. android:icon
- Biểu tượng đại diện cho broadcast receiver. Thuộc tính này được đặt ở dạng tham chiếu đến một tài nguyên có thể vẽ có chứa định nghĩa hình ảnh.
Nếu bạn không đặt thuộc tính này, thì hệ thống sẽ sử dụng biểu tượng được chỉ định cho toàn bộ ứng dụng. Hãy xem thuộc tính
icon
của phần tử<application>
.Biểu tượng của broadcast receiver, bất kể được đặt ở đây hay do phần tử
<application>
đặt, cũng đều là biểu tượng mặc định cho tất cả các bộ lọc ý định của receiver. Hãy xem thuộc tínhicon
của phần tử<intent-filter>
. android:label
- Nhãn mà người dùng đọc được cho broadcast receiver. Nếu bạn không đặt thuộc tính này, thì nhãn được đặt chung cho ứng dụng sẽ được dùng. Hãy xem thuộc tính
label
của phần tử<application>
.Nhãn của broadcast receiver, cho dù được đặt ở đây hay do phần tử
<application>
đặt, cũng đều là nhãn mặc định cho tất cả các bộ lọc ý định của receiver. Hãy xem thuộc tínhlabel
của phần tử<intent-filter>
.Nhãn được đặt ở dạng tham chiếu đến tài nguyên chuỗi để có thể được bản địa hoá như các chuỗi khác trong giao diện người dùng. Tuy nhiên, để thuận tiện cho quá trình phát triển ứng dụng, bạn cũng có thể đặt nhãn này làm chuỗi thô.
android:name
- Tên của lớp triển khai broadcast receiver, lớp con của
BroadcastReceiver
. Tên này là tên lớp đủ điều kiện, chẳng hạn như"com.example.project.ReportReceiver"
. Tuy nhiên, ở dạng viết tắt, nếu ký tự đầu tiên của tên là một dấu chấm, chẳng hạn như".ReportReceiver"
, thì hệ thống sẽ nối thêm tên gói được chỉ định trong phần tử<manifest>
.Một khi bạn đã phát hành ứng dụng thì đừng thay đổi tên này, trừ trường hợp bạn đặt
android:exported="false"
.Không có giá trị mặc định. Bạn phải chỉ định tên.
android:permission
- Tên của một quyền mà đài phát cần có để gửi thông báo cho broadcast receiver.
Nếu bạn không đặt thuộc tính này, thì quyền do thuộc tính
permission
của phần tử<application>
đặt sẽ áp dụng cho broadcast receiver. Nếu bạn không đặt thuộc tính nào, thì receiver sẽ không được bảo vệ bởi quyền.Để biết thêm thông tin về các quyền, hãy xem phần Quyền trên trang tổng quan về tệp kê khai ứng dụng và bài viết Mẹo bảo mật.
android:process
- Tên của quy trình mà broadcast receiver chạy trong đó.
Thường thì tất cả thành phần của ứng dụng sẽ chạy trong quy trình mặc định được tạo cho ứng dụng. Quy trình này có cùng tên với gói ứng dụng.
Thuộc tính
process
của phần tử<application>
có thể đặt một giá trị mặc định khác cho tất cả các thành phần. Tuy nhiên, mỗi thành phần có thể ghi đè giá trị mặc định bằng thuộc tínhprocess
riêng, cho phép bạn trải rộng ứng dụng của mình trên nhiều quy trình.Nếu tên gán cho thuộc tính này bắt đầu bằng dấu hai chấm (
:
), thì một quy trình mới (dành riêng cho ứng dụng) sẽ được tạo khi cần và broadcast receiver sẽ chạy trong quy trình đó.Nếu tên quy trình bắt đầu bằng một ký tự viết thường, thì receiver sẽ chạy trong một quy trình chung của tên đó, miễn receiver có quyền làm vậy. Điều này cho phép các thành phần trong nhiều ứng dụng dùng chung một quy trình, qua đó giúp giảm mức sử dụng tài nguyên.
- ra mắt từ:
- API cấp 1
<bộ thu>
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],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"]]