PowerManager is used in the Android platform to manage the power state of the device, reboot, Hibernate, Wake up, etc., using the API will affect the battery's standby time, so nothing is necessary, generally do not use.
There are several more important public methods in PowerManager, as follows:
Methods Gotosleep, reboot, useractivity need to use the following permissions:
<uses-permission android:name= "Android.permission.REBOOT"/>
<uses-permissionandroid:name= "Android.permission.DEVICE_POWER"/>
These two permissions are only available to the System app.
The most important method is Newwakelock, through which we can get a Wakelock object, through which we can control the state of the device power supply. The first parameter of the method flags is used to indicate which wakelock to get, and the different lock has different effects on the CPU, screen, keyboard lights, and the value of flag is as follows:
Below we use a case to illustrate the usage of wakelock, the specific code is as follows:
Public classPowermanageractivityextendsactionbaractivity {PrivatePowerManager PowerManager; PrivatePowermanager.wakelock WakeLock; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_power_manager); PowerManager=(PowerManager) Getsystemservice (Context.power_service); } Private voidAcquirewakelock () {WakeLock= Powermanager.newwakelock (Powermanager.screen_bright_wake_lock, "Wakelock"); if(wakelock!=NULL) Wakelock.acquire (); } Private voidReleasewakelock () {if(wakelock!=NULL) wakelock.release (); } @Overrideprotected voidOnresume () {Super. Onresume (); Acquirewakelock (); } @Overrideprotected voidOnPause () {Super. OnPause (); Releasewakelock (); }}
The following permissions are required to use this method:
<uses-permission android:name= "Android.permission.WAKE_LOCK"/>
The code is simple, and when we get wakelock we use Powermanager.screen_bright_wake_lock, which indicates that the CPU is running and the screen is always on. At the same time, note that Wakelock's acquire method and release method are used in pairs, and the life cycle method of activity is used here.
Also note that Wakelock is the activity level and not for the entire application.
To obtain battery information such as status, health information, battery capacity, etc., we can register a broadcast receiver to receive intent.action_battery_changed broadcast, when the battery sends changes, the system will issue the broadcast. Android provides the Batterymanager class, which provides a number of constants for extracting related data from broadcast intent, and the constants list is as follows:
Define a broadcast receiver to detect changes in battery status, the key code is as follows:
Private classBattaryreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {if(Intent.getaction (). Equals (intent.action_battery_changed)) {//Battery Status intStatus = Intent.getintextra (batterymanager.extra_status,0); String statusstring= ""; Switch(status) { CaseBatteryManager.BATTERY_STATUS_FULL:statusString= "Full"; Break; CaseBatteryManager.BATTERY_STATUS_CHARGING:statusString= "Charging"; Break; CaseBatteryManager.BATTERY_STATUS_DISCHARGING:statusString= "discharging"; Break; CaseBatteryManager.BATTERY_STATUS_UNKNOWN:statusString= "Unknown"; Break; CaseBatteryManager.BATTERY_STATUS_NOT_CHARGING:statusString= "Not Charging"; Break; } // BooleanPresent= Intent.getbooleanextra (Batterymanager.extra_present,false); //integer field containing the current battery level, intCurlevel = Intent.getintextra (batterymanager.extra_level,0); //integer containing the maximum battery level intMaxlevel = Intent.getintextra (batterymanager.extra_scale,0); //String describing the technology of the current batteryString technology =Intent.getstringextra (batterymanager.extra_technology); //integer containing the current health constant intHealth = Intent.getintextra (batterymanager.extra_health,0); String Healthstr= ""; Switch(health) { CaseBatteryManager.BATTERY_HEALTH_COLD:healthStr= "cold"; Break; CaseBatteryManager.BATTERY_HEALTH_DEAD:healthStr= "Dead"; Break; CaseBatteryManager.BATTERY_HEALTH_GOOD:healthStr= "good"; Break; CaseBatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:healthStr= "Voer voltage"; Break; CaseBatteryManager.BATTERY_HEALTH_UNKNOWN:healthStr= "Unknown"; Break; CaseBatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:healthStr= "Unspecified Failure"; Break; } //integer containing the resource ID of a small status bar icon indicating the current battery state intSmallIcon = Intent.getintextra (batterymanager.extra_icon_small,0); //integer indicating whether the device is plugged in to a power source; 0 means it's on battery,//Other constants is different types of power sources intplugged = Intent.getintextra (batterymanager.extra_plugged,0); String Pluggedstr=""; Switch(plugged) { CaseBatteryManager.BATTERY_PLUGGED_AC:pluggedStr= "AC"; Break; CaseBatteryManager.BATTERY_PLUGGED_USB:pluggedStr= "USB"; Break; CaseBatteryManager.BATTERY_PLUGGED_WIRELESS:pluggedStr= "Wireless"; Break; } //integer containing the current battery temperature. intTemperature= Intent.getintextra (batterymanager.extra_temperature,0); //integer containing the current battery voltage level. intVoltage = Intent.getintextra (batterymanager.extra_voltage,0); StringBuilder SB=NewStringBuilder ("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); }}}
Permission is required to receive the broadcast primarily:
<uses-permission android:name= "Android.permission.BATTERY_STATS"/>
The results of the case operation are as follows:
Jerry Education
Source:http://www.cnblogs.com/jerehedu/
Copyright Notice: The copyright of this article is Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Technical Consultation:
Android's Powermanager&batterymanager