BatteryService monitors the charging status, and charge level of the device battery. When these values change this service broadcasts the new values to all IntentReceivers (android.content.BroadcastReceiver) that are watching the BATTERY_CHANGED (android.content.Intent#ACTION_BATTERY_CHANGED) action.
The new values are stored in the Intent data and can be retrieved by calling Intent.getExtra (android.content.Intent#getExtra) with the following keys:
"scale" - int, the maximum value for the charge level.
"level" - int, charge level, from 0 through "scale" inclusive.
"status" - String, the current charging status.
"health" - String, the current battery health.
"present" - boolean, true if the battery is present.
"icon-small" - int, suggested small icon to use for this state.
"plugged" - int, 0 if the device is not plugged in; 1 if plugged into an AC power adapter; 2 if plugged in via USB.
"voltage" - int, current battery voltage in millivolts.
"temperature" - int, current battery temperature in tenths of a degree Centigrade.
"technology" - String, the type of battery installed, e.g. "Li-ion".
1. mUEventObserver.startObserving("SUBSYSTEM=power_supply");
private UEventObserver mUEventObserver = new UEventObserver() {
@Override
public void onUEvent(UEventObserver.UEvent event) {
update();
}
};
Observe power supply status. When there is a change, it update the battery status by invoking the update method.
2. update
The battery information is required by invoking the native_update method, which is implemented in the file frameworks/base/services/jni/com_android_server_battery_service.cpp.
In fact, all these information is required from the system file /sys/class/power_supply.
(For more infomation, we need to dig inside Linux kernel.)
static void android_server_BatteryService_update(JNIEnv* env, jobject obj)
{
setBooleanField(env, obj, gPaths.acOnlinePath, gFieldIds.mAcOnline);
setBooleanField(env, obj, gPaths.usbOnlinePath, gFieldIds.mUsbOnline);
setBooleanField(env, obj, gPaths.batteryPresentPath, gFieldIds.mBatteryPresent);
setIntField(env, obj, gPaths.batteryCapacityPath, gFieldIds.mBatteryLevel);
setVoltageField(env, obj, gPaths.batteryVoltagePath, gFieldIds.mBatteryVoltage);
setIntField(env, obj, gPaths.batteryTemperaturePath, gFieldIds.mBatteryTemperature);
const int SIZE = 128;
char buf[SIZE];
if (readFromFile(gPaths.batteryStatusPath, buf, SIZE) > 0)
env->SetIntField(obj, gFieldIds.mBatteryStatus, getBatteryStatus(buf));
else
env->SetIntField(obj, gFieldIds.mBatteryStatus,
gConstants.statusUnknown);
if (readFromFile(gPaths.batteryHealthPath, buf, SIZE) > 0)
env->SetIntField(obj, gFieldIds.mBatteryHealth, getBatteryHealth(buf));
if (readFromFile(gPaths.batteryTechnologyPath, buf, SIZE) > 0)
env->SetObjectField(obj, gFieldIds.mBatteryTechnology, env->NewStringUTF(buf));
}
static JNINativeMethod sMethods[] = {
/* name, signature, funcPtr */
{"native_update", "()V", (void*)android_server_BatteryService_update},
};
--------------------------------------------------
Also, we could see that two method will be invoked. From the name you will know what they are doing.
shutdownIfNoPower();
shutdownIfOverTemp();
private final void shutdownIfOverTemp() {
// shut down gracefully if temperature is too high (> 68.0C)
// wait until the system has booted before attempting to display the shutdown dialog.
if (mBatteryTemperature > 680 && ActivityManagerNative.isSystemReady()) {
Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
}
Finally, if either status changes, it will notify the observers.
分享到:
相关推荐
The.Illustrated.Network_.How.TCP_IP.Works.in.a.Modern.Network.2008.0123745411.pdf
How it works... There's more... See also Reader feedback Customer support Downloading the example code Downloading the color images of this book Errata Piracy Questions 1. Pandas Foundations ...
这份资料以PDF格式压缩在名为"How Software Works.pdf.zip"的压缩包中,标签简称为"How Software Wor",其中包含的主要文件为"How Software Works.pdf"。 软件的工作原理涵盖了多个层面,包括但不限于编程语言、...
《How Tomcat Works》中文版一书详细剖析了Tomcat服务器的内部工作机制。该书基于Tomcat 4.1.12和5.0.18两个版本,深入讲解了其servlet容器的架构和运作原理,尤其是代号为Catalina的核心组件。 Tomcat是一个开源的...
tomcat工作原理深入详解——HowTomcatWorks中文版.pdf
《CMOS Electronics How It Works How It Fails》这本书深入剖析了这些内容,为读者提供了从基础理论到实际应用的全面知识。通过学习,读者不仅可以理解CMOS电子如何在无线通信中发挥作用,还能掌握如何预防和解决...
Android, the next-generation open mobile platform from Google and the Open Handset Alliance, is poised to become a significant player in the mobile device market. The Android platform gives developers...
Title: Android Programming, 2nd Edition Author: Kevin Lyn. Length: 163 pages Edition: 2 Language: English Publication Date: 2015-04-23 ISBN-10: B00WMRMW8C ...Chapter 6: Advanced features in Android
How to create projects in Android Studio How to use the SDK manager to keep your Android SDK current How to commit and get projects to and from Git hub How to use OpenGL ES to load images How to react...
The Illustrated Network How TCPIP Works in a Modern Network(2nd) 英文无水印原版pdf 第2版 pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络,如有侵权...
[互联网工作原理].Que,.How.the.Internet.Works.(2006),8Ed.chm
You will also create beautiful and responsive menus and dialogs and explore the different options for playing sound effects and music in Android. You will then learn the basics of creating a ...
Ultimately, this invaluable resource provides in–depth, runnable code examples that you can then adapt for your own applications.Shows experienced Android developers how to exploit the rich set of ...
“Android™ Database Best Practices goes well beyond API documentation to offer strategic advice about how to handle data in an Android application and the tools needed to develop productively....
OS.Build Reader This is a development tool to show values of android.os.Build.* ...For more information about how to manage AVDs, see developer.android.com Startup emulator now, startup your avd using
动画讲解计算机CPU工作原理,英文讲解中文字幕机器翻译令人忍俊不禁