Android PowerManager & BatteryManager, powermanager
PowerManager is used in the Android platform to manage and control the power status, restart, sleep status, and wake up of the device. Using this API will affect the battery standby time, so it is only necessary and generally not used.
There are several important common methods in PowerManager:
The following permissions are required for methods such as goToSleep, reboot, and userActivity:
<uses-permission android:name="android.permission.REBOOT"/>
<uses-permissionandroid:name="android.permission.DEVICE_POWER"/>
These two permissions can only be used by the system App.
The most important method is newWakeLock. Through this method, we can obtain a WakeLock object, through which we can control the power status of the device. The first flags parameter of the method is used to indicate which WakeLock to be obtained. Different locks have different effects on the cpu, screen, and keyboard lamp. The flag value is as follows:
The following uses a case to describe how to use WakeLock. The specific code is as follows:
public class PowerManagerActivity extends ActionBarActivity { private PowerManager powerManager; private PowerManager.WakeLock wakeLock; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_power_manager); powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE); } private void acquireWakeLock(){ wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,"wakelock"); if(wakeLock!=null) wakeLock.acquire(); } private void releaseWakeLock(){ if(wakeLock!=null) wakeLock.release(); } @Override protected void onResume() { super.onResume(); acquireWakeLock(); } @Override protected void onPause() { super.onPause(); releaseWakeLock(); }}
The following permissions are required to use this method:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
The code is relatively simple. We use PowerManager. SCREEN_BRIGHT_WAKE_LOCK when obtaining WakeLock. This flag indicates that the cpu remains running and the screen remains on. At the same time, note that the acquire method and the release method of WakeLock must be used in pairs. Here, the Activity lifecycle method is used.
It should also be noted that WakeLock is Activity-level rather than for the entire Application.
To obtain battery information such as status, health information, and battery capacity, you can register a broadcast receiver to receive Intent. ACTION_BATTERY_CHANGED broadcast. the broadcast is sent when the battery sends a change. Android provides the BatteryManager class, which mainly provides constants for Extracting related data from the broadcast Intent. The constant list is as follows:
Define a broadcast receiver to detect changes in the battery status. The key code is as follows:
Private class BattaryReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {if (intent. getAction (). equals (Intent. ACTION_BATTERY_CHANGED) {// Battery status int status = intent. getIntExtra (BatteryManager. EXTRA_STATUS, 0); String statusString = ""; switch (status) {case BatteryManager. BATTERY_STATUS_FULL: statusString = "full"; break; case BatteryManager. BATTERY_STATUS_CHARGING: statusString = "charging"; break; case BatteryManager. BATTERY_STATUS_DISCHARGING: statusString = "discharging"; break; case BatteryManager. BATTERY_STATUS_UNKNOWN: statusString = "unknown"; break; case BatteryManager. BATTERY_STATUS_NOT_CHARGING: statusString = "not charging"; break;} // boolean present = intent. getBooleanExtra (BatteryManager. EXTRA_PRESENT, false); // integer field containing the current battery level, int curlevel = intent. getIntExtra (BatteryManager. EXTRA_LEVEL, 0); // integer containing the maximum battery level int maxLevel = intent. getIntExtra (BatteryManager. EXTRA_SCALE, 0); // String describing the technology of the current battery String technology = intent. getStringExtra (BatteryManager. EXTRA_TECHNOLOGY); // integer containing the current health constant int health = intent. getIntExtra (BatteryManager. EXTRA_HEALTH, 0); String healthStr = ""; switch (health) {case BatteryManager. BATTERY_HEALTH_COLD: healthStr = "cold"; break; case BatteryManager. BATTERY_HEALTH_DEAD: healthStr = "dead"; break; case BatteryManager. BATTERY_HEALTH_GOOD: healthStr = "good"; break; case BatteryManager. BATTERY_HEALTH_OVER_VOLTAGE: healthStr = "voer voltage"; break; case BatteryManager. BATTERY_HEALTH_UNKNOWN: healthStr = "unknown"; break; case BatteryManager. BATTERY_HEALTH_UNSPECIFIED_FAILURE: healthStr = "unspecified failure"; break;} // integer containing the resource ID of a small status bar icon indicating the current battery state int smallIcon = intent. getIntExtra (BatteryManager. EXTRA_ICON_SMALL, 0); // integer indicating whether the device is plugged in to a power source; 0 means it is on battery, // other constants are different types of power sources int plugged = intent. getIntExtra (BatteryManager. EXTRA_PLUGGED, 0); String pluggedStr = ""; switch (plugged) {case BatteryManager. BATTERY_PLUGGED_AC: pluggedStr = "AC"; break; case BatteryManager. BATTERY_PLUGGED_USB: pluggedStr = "USB"; break; case BatteryManager. BATTERY_PLUGGED_WIRELESS: pluggedStr = "wireless"; break;} // integer containing the current battery temperature. int temperature = intent. getIntExtra (BatteryManager. EXTRA_TEMPERATURE, 0); // integer containing the current battery voltage level. int voltage = intent. getIntExtra (BatteryManager. EXTRA_VOLTAGE, 0); StringBuilder sb = new StringBuilder ("status:"); sb. append (statusString); sb. append ("\ n"); sb. append ("present :"). append (present); sb. append ("\ n"); sb. append ("level :"). append (curlevel); sb. append ("\ n"); sb. append ("maxLevel :"). append (maxLevel); sb. append ("\ n"); sb. append ("technology :"). append (technology); sb. append ("\ n"); sb. append ("health :"). append (healthStr); sb. append ("\ n"); sb. append ("icon :"). append (smallIcon); sb. append ("\ n"); sb. append ("plugged :"). append (pluggedStr); sb. append ("\ n"); sb. append ("temperature :"). append (temperature); sb. append ("\ n"); sb. append ("voltage :"). append (voltage); sb. append ("\ n"); Message msg = mHandler. obtainMessage (1); msg. obj = sb. toString (); mHandler. sendMessage (msg );}}}
Permissions are required to receive the broadcast:
<uses-permission android:name="android.permission.BATTERY_STATS"/>
The running result of the case is as follows:
Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation: