- ไวยากรณ์:
<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>
- description:
- ประกาศ Broadcast Receiver ซึ่งเป็น
BroadcastReceiver
คลาสย่อย เป็นหนึ่งในคอมโพเนนต์ของแอปพลิเคชัน เปิดใช้ Broadcast Receiver อยู่ แอปพลิเคชันเพื่อรับ Intent ที่เผยแพร่โดยระบบหรือโดย แม้ว่าคอมโพเนนต์อื่นๆ ของแอปพลิเคชันจะไม่ได้ทำงานอยู่ก็ตามการทำให้ระบบรู้จัก Broadcast Receiver ได้ 2 วิธี ประการแรกคือ ให้ประกาศในไฟล์ Manifest พร้อมด้วยเอลิเมนต์นี้ อีกวิธีคือการสร้าง ผู้รับแบบไดนามิกในโค้ดและลงทะเบียนไว้กับ
Context.registerReceiver()
หรือเมธอดใดเวอร์ชันหนึ่งที่โอเวอร์โหลดสำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีการสร้างตัวรับแบบไดนามิก โปรดดู
BroadcastReceiver
ชั้นเรียน คำอธิบายหากตัวรับสัญญาณนี้จัดการการออกอากาศที่ไม่ใช่ระบบ ให้ระบุค่าสำหรับ
android:exported
ตั้งค่านี้เป็น"true"
หากคุณต้องการให้ผู้รับสามารถ ตัวรับสัญญาณจากแอปพลิเคชันอื่นหรือ"false"
หากคุณ ต้องการให้ผู้รับรับการออกอากาศจากแอปของคุณเองได้คุณไม่จำเป็นต้องนำแอตทริบิวต์
android:permission
ออกหาก ได้ประกาศไปแล้วคำเตือน: จำกัดจำนวนการออกอากาศ รีซีฟเวอร์ที่คุณตั้งค่าไว้ในแอป การมี Broadcast Receiver มากเกินไปอาจ ส่งผลต่อประสิทธิภาพการทำงานของแอปและอายุการใช้งานแบตเตอรี่ของผู้ใช้ อุปกรณ์ สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ API ที่คุณสามารถใช้แทน
BroadcastReceiver
ชั้นเรียนสำหรับการจัดตารางเวลางานในเบื้องหลัง โปรดดู การเพิ่มประสิทธิภาพในเบื้องหลัง - ดังนี้
android:directBootAware
ตัวรับสัญญาณบรอดแคสต์นั้นรับรู้การเปิดเครื่องโดยตรงหรือไม่ คืออุปกรณ์จะทำงานก่อนที่ผู้ใช้จะปลดล็อกอุปกรณ์
หมายเหตุ: ระหว่าง Direct Boot (การเปิดเครื่องโดยตรง) ซึ่งเป็นการออกอากาศ ตัวรับสัญญาณในแอปพลิเคชันของคุณ สามารถเข้าถึงเฉพาะข้อมูลที่เก็บไว้ใน พื้นที่เก็บข้อมูลที่ปกป้องอุปกรณ์
ค่าเริ่มต้นคือ
"false"
android:enabled
- ระบบสร้างอินสแตนซ์ Broadcast Receiver ได้หรือไม่ ตอนนี้
"true"
หากทำได้ และ"false"
หากทำได้ ค่าเริ่มต้น มีค่า"true"
องค์ประกอบ
<application>
ของตัวเอง แอตทริบิวต์enabled
ที่ใช้กับแอตทริบิวต์ทั้งหมด คอมโพเนนต์ต่างๆ ของแอปพลิเคชัน รวมถึง Broadcast Receiver<application>
และ แอตทริบิวต์<receiver>
ต้องเป็น"true"
ทั้งคู่ Broadcast Receiver จะเปิดใช้ หากข้อใดข้อหนึ่งคือ"false"
ค่า ปิดใช้งานและไม่สามารถสร้างอินสแตนซ์ได้ android:exported
- Broadcast Receiver รับข้อความจากแหล่งที่มาที่ไม่ใช่ระบบได้หรือไม่
ภายนอกแอปพลิเคชัน ราคา
"true"
หากทำได้ และ"false"
หากไม่ หากเป็น"false"
ข้อความเดียวที่ Broadcast Receiver จะส่งข้อความ ผู้รับหมายถึงข้อความที่ส่งมาโดยระบบ คอมโพเนนต์ของแอปพลิเคชัน หรือแอปพลิเคชันเดียวกัน ด้วย User-ID เดียวกันหากไม่ระบุ ค่าเริ่มต้นจะขึ้นอยู่กับว่า Broadcast Receiver มี Intent หรือไม่ ตัวกรอง หากฝั่งผู้รับมีตัวกรอง Intent อย่างน้อย 1 รายการ ค่าเริ่มต้นจะเป็น
"true"
หากไม่ได้กำหนด ค่าเริ่มต้นจะเป็น"false"
แอตทริบิวต์นี้ไม่ใช่วิธีเดียวในการจำกัดการรับแสงภายนอกของ Broadcast Receiver คุณยังใช้สิทธิ์เพื่อจำกัดเอนทิตีภายนอกที่ส่งข้อความได้ ดูแอตทริบิวต์
permission
android:icon
- ไอคอนที่แสดงถึง Broadcast Receiver ตั้งค่าแอตทริบิวต์นี้แล้ว
เป็นการอ้างอิงไปยังทรัพยากรที่ถอนออกได้ที่มีคำจำกัดความของรูปภาพ
หากไม่ได้ตั้งค่า ระบบจะใช้ไอคอนที่ระบุสำหรับแอปพลิเคชันโดยรวม
แทน โปรดดู
<application>
แอตทริบิวต์icon
ขององค์ประกอบไอคอนตัวรับสัญญาณออกอากาศ ไม่ว่าจะตั้งค่าไว้ที่นี่หรือตามการตั้งค่า
<application>
ยังเป็นองค์ประกอบ ไอคอนเริ่มต้นสำหรับตัวกรอง Intent ของผู้รับทั้งหมด โปรดดู ขององค์ประกอบ<intent-filter>
icon
android:label
- ป้ายกำกับที่ผู้ใช้อ่านได้สำหรับ Broadcast Receiver หากแอตทริบิวต์นี้ไม่ใช่
ป้ายกำกับที่ตั้งไว้สำหรับแอปพลิเคชันโดยรวมคือ
ใช้แทน ดูองค์ประกอบ
<application>
label
ป้ายกำกับของผู้รับออกอากาศ ไม่ว่าจะตั้งค่าไว้ที่นี่หรือตามการตั้งค่า
<application>
ยังเป็นองค์ประกอบ ป้ายกำกับเริ่มต้นสำหรับตัวกรอง Intent ของผู้รับทั้งหมด โปรดดู ขององค์ประกอบ<intent-filter>
label
ป้ายกำกับได้รับการตั้งค่าเป็นการอ้างอิงไปยังทรัพยากรสตริง ดังนั้น สามารถแปลได้เหมือนกับสตริงอื่นๆ ในอินเทอร์เฟซผู้ใช้ แต่เพื่อความสะดวก ในระหว่างที่คุณพัฒนาแอปพลิเคชัน ยังสามารถตั้งค่าเป็นสตริงดิบได้
android:name
- ชื่อคลาสที่ใช้งาน Broadcast Receiver ซึ่งเป็นคลาสย่อยของ
BroadcastReceiver
นี่เป็นคุณสมบัติที่สมบูรณ์ ชื่อชั้นเรียน เช่น"com.example.project.ReportReceiver"
อย่างไรก็ตาม เหมือนชวเลข ถ้าอักขระตัวแรกของชื่อคือจุด เช่น".ReportReceiver"
ต่อท้ายชื่อแพ็กเกจที่ระบุไว้ใน องค์ประกอบ<manifest>
เมื่อคุณเผยแพร่แอปพลิเคชันของคุณแล้ว อย่า เปลี่ยนชื่อนี้ เว้นแต่คุณจะตั้งค่า
android:exported="false"
ไม่มีค่าเริ่มต้น ต้องระบุชื่อ
android:permission
- ชื่อสิทธิ์ที่ผู้ออกอากาศจำเป็นต้องมีเพื่อส่ง
ไปยัง Broadcast Receiver
หากไม่ได้ตั้งค่าแอตทริบิวต์นี้ไว้ สิทธิ์ที่กำหนดโดย
ขององค์ประกอบ
<application>
ใช้แอตทริบิวต์permission
รายการ ไปยัง Broadcast Receiver หากไม่ได้ตั้งค่าแอตทริบิวต์ไว้ ตัวรับสัญญาณ ไม่ได้รับการคุ้มครองโดยสิทธิ์ดูข้อมูลเพิ่มเติมเกี่ยวกับสิทธิ์ได้ที่ สิทธิ์ ในภาพรวมไฟล์ Manifest ของแอปและ เคล็ดลับความปลอดภัย
android:process
- ชื่อกระบวนการที่ Broadcast Receiver ทำงาน
โดยปกติแล้ว คอมโพเนนต์ทั้งหมดของแอปพลิเคชันจะทำงานในกระบวนการเริ่มต้นที่สร้างขึ้น
สำหรับแอปพลิเคชัน มีชื่อเดียวกับแพ็กเกจแอปพลิเคชัน
ขององค์ประกอบ
<application>
แอตทริบิวต์process
สามารถตั้งค่า ค่าเริ่มต้นสำหรับทุกคอมโพเนนต์ แต่คอมโพเนนต์แต่ละอย่างสามารถแทนที่ค่าเริ่มต้น ด้วยแอตทริบิวต์process
ของตัวเองเพื่อ ช่วยให้คุณกระจายข้อมูล การใช้งานในหลายกระบวนการหากชื่อที่กำหนดให้กับแอตทริบิวต์นี้เริ่มต้นด้วยโคลอน (
:
) ซึ่งเป็นแบบส่วนตัวสำหรับแอปพลิเคชัน จะสร้างขึ้นเมื่อจำเป็น และ Broadcast Receiver จะทำงานในกระบวนการนั้นหากชื่อกระบวนการเริ่มต้นด้วยอักขระตัวพิมพ์เล็ก ตัวรับจะทำงาน ในกระบวนการส่วนกลางของชื่อนั้น หากชื่อนั้นได้รับอนุญาตให้ดำเนินการดังกล่าว ทำให้คอมโพเนนต์ต่างๆ ในแอปพลิเคชันต่างๆ สามารถแบ่งปันกระบวนการได้โดย การใช้ทรัพยากร
- เปิดตัวใน
- API ระดับ 1
<ตัวรับ>
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 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"]]