Android之PowerManager&BatteryManager

来源:转载

PowerManager是Android平台中用于管理控制设备电源状态、重启、休眠状态、唤醒等,使用该API会影响到电池的待机时间,所以无非必要,一般不要使用。

在PowerManager中有几个比较重要的公共方法,具体如下:

方法goToSleep、reboot、userActivity需要用到如下权限:

<uses-permission android:name="android.permission.REBOOT"/>

<uses-permissionandroid:name="android.permission.DEVICE_POWER"/>

而这两个权限只有系统App才可使用。

最重要的方法还是newWakeLock,通过该方法,我们可以获取一个WakeLock对象,通过该对象我们可以控制设备电源的状态。方法的第一个参数flags用于指示获取哪种WakeLock,不同的Lock对cpu、屏幕、键盘灯有不同的影响,flag的值如下:

下面我们通过一个案例来说明WakeLock的用法,具体代码如下:

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(); }}
 使用该方法需要用到如下权限:

<uses-permission android:name="android.permission.WAKE_LOCK"/>

 代码比较简单,在获取WakeLock的时候我们使用了PowerManager.SCREEN_BRIGHT_WAKE_LOCK,该flag表明cpu保持运行,屏幕保持常亮。同时注意WakeLock的acquire方法和release方法要成对使用,在这里利用了Activity的生命周期方法。
 还要注意的是WakeLock是Activity级别的不是针对整个Application的。
 要想获取电池信息比如状态、健康信息、电池容量等,我们可以通过注册一个广播接收者用来接收Intent.ACTION_BATTERY_CHANGED广播即可,当电池发送变化的时候,系统就会发出该广播。Android提供了BatteryManager类,该类主要提供了一些常量,用于从广播的Intent中提取相关的数据,常量列表如下:

定义一个广播接收者,用于检测电池状态的变化,关键代码如下:
private class BattaryReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){ //电池状态 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); }}}
主要接收该广播需要用到权限:

<uses-permission android:name="android.permission.BATTERY_STATS"/>

案例运行结果如下:

 

作者:杰瑞教育

出处:http://www.cnblogs.com/jerehedu/ 版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
技术咨询: 



分享给朋友:
您可能感兴趣的文章:
随机阅读: