ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
รองรับอุปกรณ์ที่ไม่มีแบตเตอรี่
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หน้านี้จะอธิบายวิธีที่ Android จัดการผลิตภัณฑ์ที่มีแบตเตอรี่แบบถอดออกได้หรือไม่มีแบตเตอรี่ภายใน อุปกรณ์หลังจะเชื่อมต่อกับแหล่งจ่ายไฟภายนอกแทน เช่น เต้ารับ AC หรือพอร์ต USB บนอุปกรณ์อื่น
มีแบตเตอรี่อยู่ไหม
แอปพลิเคชันอาจใช้โค้ดต่อไปนี้เพื่อตรวจหาว่าอุปกรณ์มีแบตเตอรี่หรือไม่
```
final Intent batteryInfo = registerReceiver(null, new
IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return batteryInfo.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
```
ลักษณะการทํางานของอุปกรณ์ที่ไม่มีแบตเตอรี่
หาก Android ไม่พบอุปกรณ์แบตเตอรี่สำหรับผลิตภัณฑ์ ระบบจะใช้ค่าเริ่มต้นต่อไปนี้ที่เกี่ยวข้องกับแบตเตอรี่ โปรดทราบว่าค่าเริ่มต้นมีการเปลี่ยนแปลงในรุ่น Android 9 ตารางนี้แสดงความแตกต่าง
สถานะแบตเตอรี่
|
Android 9 ขึ้นไป
|
Android 8.1 และต่ำกว่า
|
นำเสนอ
|
เท็จ
|
true
|
สถานะ
|
ไม่ทราบ
|
กำลังชาร์จ
|
ความจุที่เหลือ
|
0
|
100%
|
สุขภาพ
|
ไม่ทราบ
|
ดี
|
สถานะออนไลน์ของที่ชาร์จ AC
|
ไม่ได้แก้ไข
|
บังคับให้เป็น "จริง"
|
ผู้ผลิตอาจเปลี่ยนแปลงการตั้งค่าเริ่มต้นโดยใช้ไดรเวอร์เคอร์เนล power_supply หรือ Health HAL
Android 9 ขึ้นไป
Android 9 นำโค้ดก่อนหน้านี้บางส่วนสำหรับอุปกรณ์ที่ไม่มีแบตเตอรี่ออก ซึ่งโดยค่าเริ่มต้นจะแกล้งทำเป็นว่ามีแบตเตอรี่ กำลังชาร์จ 100% และอยู่ในสถานะดีพร้อมการอ่านอุณหภูมิปกติในเทอร์มิสเตอร์
API เฟรมเวิร์กส่วนใหญ่ที่จัดการข้อมูลนี้จะยังคงจัดการสถานการณ์ทั่วไปเหมือนเดิม นั่นคือ ระบบจะถือว่ากำลังชาร์จ (ไม่ได้ใช้พลังงานจากแบตเตอรี่) และจะไม่ถือว่ามีแบตเตอรี่เหลือน้อย หากอินเทอร์เฟซผู้ใช้วาดไอคอนแบตเตอรี่ ไอคอนดังกล่าวจะปรากฏขึ้นพร้อมเครื่องหมายตกใจ และเปอร์เซ็นต์แบตเตอรี่จะแสดงเป็น 0%
แต่อุปกรณ์จะไม่ปิดเนื่องจากแบตเตอรี่เหลือน้อย และระบบจะกำหนดเวลางานที่ต้องชาร์จแบตเตอรี่หรือแบตเตอรี่ต้องเต็ม
Android 8.1 และต่ำกว่า
เนื่องจากไม่ทราบสถานะแบตเตอรี่ API ของเฟรมเวิร์ก Android จะถือว่าระบบกำลังชาร์จ (หรือไม่ได้ทำงานโดยใช้พลังงานจากแบตเตอรี่) และจะไม่ถือว่าแบตเตอรี่เหลือน้อย หากอินเทอร์เฟซผู้ใช้แสดงผลไอคอนแบตเตอรี่ ไอคอนจะปรากฏขึ้นพร้อมกับเครื่องหมายตกใจ และเปอร์เซ็นต์แบตเตอรี่จะแสดงเป็น 0% แต่อุปกรณ์จะไม่ปิดเนื่องจากแบตเตอรี่เหลือน้อย และระบบจะกำหนดเวลางานที่ต้องชาร์จหรือแบตเตอรี่เต็ม
การใช้งาน
โค้ดเริ่มต้นของ Android 9 อาจทํางานได้อย่างถูกต้องกับอุปกรณ์ของคุณ แต่ขอแนะนําให้ทําการเปลี่ยนแปลงเคอร์เนลหรือ HAL เพื่อแสดงสถานะพลังงานและแบตเตอรี่ของผลิตภัณฑ์อย่างถูกต้องตามที่อธิบายไว้ก่อนหน้านี้ หาก Android 9 ขึ้นไปไม่ตรวจพบอุปกรณ์ชาร์จประเภทแหล่งจ่ายไฟ Linux อุปกรณ์ชาร์จทุกประเภท (AC, USB, ไร้สาย) จะมีสถานะออฟไลน์โดยค่าเริ่มต้น หากที่ชาร์จทั้งหมดออฟไลน์แต่ไม่มีการตรวจพบอุปกรณ์ที่ใช้แบตเตอรี่ ระบบจะยังคงถือว่าอุปกรณ์กำลังชาร์จอยู่ ในแง่ที่ว่าอุปกรณ์ทำงานโดยใช้แหล่งจ่ายไฟภายนอก ไม่ใช่แบตเตอรี่ ตามที่อธิบายไว้ก่อนหน้านี้
หากผลิตภัณฑ์ไม่มีแบตเตอรี่และเชื่อมต่อกับแหล่งจ่ายไฟอยู่เสมอ คุณควรใช้ไดรเวอร์ charger คลาส power_supply ของเคอร์เนล Linux สำหรับแหล่งจ่ายไฟ AC หรือ USB ที่กำหนดแอตทริบิวต์ sysfs
online เป็น true
หรือจะกำหนดค่าพร็อพเพอร์ตี้ออนไลน์ของที่ชาร์จ AC ใน HAL ของ Health สำหรับอุปกรณ์ก็ได้ หากต้องการกำหนดค่าพร็อพเพอร์ตี้ออนไลน์ของที่ชาร์จ AC ใน HAL ของ Health โปรดดูHealth AIDL HAL
HAL สำหรับสุขภาพที่กำหนดเองนี้ใช้ Health::getHealthInfo()
เวอร์ชันที่กำหนดเองซึ่งแก้ไขค่าของ BatteryProperties.chargerAcOnline = true
ในการเริ่มต้นใช้งาน ให้คัดลอกไฟล์ hardware/interfaces/health/aidl/default/Health.cpp
ไปยังการใช้งาน Health HAL ของคุณเอง แล้วแก้ไขตาม Health AIDL HAL
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา 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,["# Support batteryless devices\n\nThis page describes how Android handles products that have either removable\nbatteries or no internal batteries. The latter devices are instead connected to\nan external power source, such as an AC power outlet or USB port on another\ndevice.\n\nIs a battery present?\n---------------------\n\nThe following code may be used by applications to detect whether the device has\na battery present: \n\n ```\n final Intent batteryInfo = registerReceiver(null, new\n IntentFilter(Intent.ACTION_BATTERY_CHANGED));\n\n return batteryInfo.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);\n ```\n\nBatteryless device behavior\n---------------------------\n\nIf Android does not detect a battery device for your product, then the following\nbattery-related default values are used. Note the defaults have changed in the\nAndroid 9 release. This table shows the differences.\n\n| Battery state | Android 9 and higher | Android 8.1 and lower |\n|----------------------------|----------------------|-----------------------|\n| *Present* | false | true |\n| *Status* | unknown | charging |\n| *Remaining capacity* | 0 | 100% |\n| *Health* | unknown | good |\n| *AC charger online status* | not modified | forced to true |\n\nManufacturers may alter the default settings using a kernel\n[power_supply](https://www.kernel.org/doc/Documentation/power/power_supply_class.txt)\ndriver or [Health HAL](/docs/core/perf/health).\n\n### Android 9 and higher\n\nAndroid 9 removes some previous code for batteryless\ndevices that by default pretended a battery was present, was being charged at\n100%, and was in good health with a normal temperature reading on its\nthermistor.\n\nMost framework APIs that deal with this information continue to handle common\nsituations the same as previously: the system is considered to be\n*charging* (that is, not running on battery power), and won't be considered\nto have a low battery. If the user interface draws the battery icon, it will\nappear with an exclamation point, and battery percentage is shown as 0%.\nBut the device won't shut down due to low battery, and jobs that require\ncharging or good battery are scheduled.\n\n### Android 8.1 and lower\n\nBecause the battery status is unknown, the Android framework APIs will consider\nthe system to be *charging* (or, not running on battery power) and won't be\nconsidered to have a low battery. If the user interface renders the battery\nicon, it will appear with an exclamation point, and battery percentage is\nshown as 0%. But the device won't shut down due to low battery, and jobs that\nrequire charging or good battery are scheduled.\n\nImplementation\n--------------\n\nThe Android 9 default code may work properly for your\ndevice, but it's recommended to make either a kernel or a HAL change to\naccurately reflect the power and battery state for your product, as described\npreviously. If Android 9 and higher doesn't detect a [Linux power supply\nclass](https://www.kernel.org/doc/Documentation/power/power_supply_class.txt)\ncharger device, then by default all charger types (AC, USB, Wireless) will have\nstatus *offline*. If all chargers are offline but there is no battery device\ndetected, the system will still be considered to be charging in the sense that\nit is running on external, not battery power, as described previously.\n\nIf your product doesn't have a battery and is always connected to a power\nsource, it's best to implement a Linux kernel power_supply class *charger*\ndriver for the AC or USB power source that sets its *online* `sysfs` attribute\nto `true`. Or you can configure the AC charger online property in a Health HAL\nfor your device. To configure the AC charger online property in a Health HAL,\nrefer to [Health AIDL HAL](https://android.googlesource.com/platform/hardware/interfaces/+/main/health/aidl/README.md).\n\nThis custom Health HAL implements a custom version of `Health::getHealthInfo()`\nthat modifies the value of `BatteryProperties.chargerAcOnline = true`.\n\nTo get started, copy file\n[`hardware/interfaces/health/aidl/default/Health.cpp`](https://android.googlesource.com/platform/hardware/interfaces/+/main/health/aidl/default/Health.cpp)\nto your own Health HAL implementation and modify it according to the\n[Health AIDL HAL](https://android.googlesource.com/platform/hardware/interfaces/+/main/health/aidl/README.md)."]]