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 ...
How Software Works How Software Works How Software Works
how tomcat works中文版.pdf百度网盘下载地址
how-tomcat-works中文版.pdf
tomcat工作原理深入详解——HowTomcatWorks中文版.pdf
How Not to Program in C++.pdf
Learn Android Studio covers Android Studio and its rich tools ecosystem, including Git and Gradle: this book covers how Android Studio works seamlessly with Git, for source control, and Gradle, a ...
tomcat的基本思想,学习完可以对理解spring的基本原理有大致了解,很值得学习
How to Use Buses in Simulink.zip
wireless communication
How 802.11 Wireless Works.doc
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...
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