Monitoring the Battery Level and Charging State

  When you're altering the frequency of your background updates to reduce the effect of those updates on battery life, checking the current battery level and charging state is a good place to start.

  The battery-life impact of performing application updates depends on the battery level and charging state of the device. The impact of performing updates while the device is charging over AC is negligible, so in most cases you can maximize your refresh rate whenever the device is connected to a wall charger. Conversely, if the device is discharging, reducing your update rate helps prolong the battery life.

  Similarly, you can check the battery charge level, potentially reducing the frequency of—or even stopping—your updates when the battery charge is nearly exhausted.

Determine the Current Charging State

  Start by determining the current charge status. The BatteryManager broadcasts all battery and charging details in a sticky Intent that includes the charging status.

  Because it's a sticky intent, you don't need to register a BroadcastReceiver—by simply callingregisterReceiver passing in null as the receiver as shown in the next snippet, the current battery status intent is returned. You could pass in an actual BroadcastReceiver object here, but we'll be handling updates in a later section so it's not necessary.

  1. IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  2. Intent batteryStatus = context.registerReceiver(null, ifilter);

  You can extract both the current charging status and, if the device is being charged, whether it's charging via USB or AC charger:

  1. // Are we charging / charged?
  2. int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
  3. boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
  4. status == BatteryManager.BATTERY_STATUS_FULL;
  5.  
  6. // How are we charging?
  7. int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
  8. boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
  9. boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

  Typically you should maximize the rate of your background updates in the case where the device is connected to an AC charger, reduce the rate if the charge is over USB, and lower it further if the battery is discharging.

Monitor Changes in Charging State

  The charging status can change as easily as a device can be plugged in, so it's important to monitor the charging state for changes and alter your refresh rate accordingly.

The BatteryManager broadcasts an action whenever the device is connected or disconnected from power. It's important to receive these events even while your app isn't running—particularly as these events should impact how often you start your app in order to initiate a background update—so you should register aBroadcastReceiver in your manifest to listen for both events by defining the ACTION_POWER_CONNECTEDand ACTION_POWER_DISCONNECTED within an intent filter.

  1. <receiver android:name=".PowerConnectionReceiver">
  2. <intent-filter>
  3. <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
  4. <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
  5. </intent-filter>
  6. </receiver>

  Within the associated BroadcastReceiver implementation, you can extract the current charging state and method as described in the previous step.

  1. public class PowerConnectionReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -);
  5. boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
  6. status == BatteryManager.BATTERY_STATUS_FULL;
  7.  
  8. int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -);
  9. boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
  10. boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
  11. }
  12. }

Determine the Current Battery Level

  In some cases it's also useful to determine the current battery level. You may choose to reduce the rate of your background updates if the battery charge is below a certain level.

  You can find the current battery charge by extracting the current battery level and scale from the battery status intent as shown here:

  1. int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -);
  2. int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -);
  3.  
  4. float batteryPct = level / (float)scale;

Monitor Significant Changes in Battery Level

  You can't easily continually monitor the battery state, but you don't need to.

Generally speaking, the impact of constantly monitoring the battery level has a greater impact on the battery than your app's normal behavior, so it's good practice to only monitor significant changes in battery level—specifically when the device enters or exits a low battery state.

  The manifest snippet below is extracted from the intent filter element within a broadcast receiver. The receiver is triggered whenever the device battery becomes low or exits the low condition by listening forACTION_BATTERY_LOW and ACTION_BATTERY_OKAY.

  1. <receiver android:name=".BatteryLevelReceiver">
  2. <intent-filter>
  3. <action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
  4. <action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>
  5. </intent-filter>
  6. </receiver>

  It is generally good practice to disable all your background updates when the battery is critically low. It doesn't matter how fresh your data is if the phone turns itself off before you can make use of it.

  In many cases, the act of charging a device is coincident with putting it into a dock. The next lesson shows you how to determine the current dock state and monitor for changes in device docking.

Android 性能优化(12)网络优化( 8)Monitoring the Battery Level and Charging State的更多相关文章

  1. Android性能优化典范(二)

    Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...

  2. android app性能优化大汇总(google官方Android性能优化典范 - 第2季)

    Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...

  3. Android性能优化典范 - 第2季

    Google发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的缩放,缓 ...

  4. Android 性能优化探究

    使用ViewStub动态载入布局.避免一些不常常的视图长期握住引用: ViewStub的一些特点: 1. ViewStub仅仅能Inflate一次,之后ViewStub对象被置空:某个被ViewStu ...

  5. android 性能优化

    本章介绍android高级开发中,对于性能方面的处理.主要包括电量,视图,内存三个性能方面的知识点. 1.视图性能 (1)Overdraw简介 Overdraw就是过度绘制,是指在一帧的时间内(16. ...

  6. Android性能优化典范第二季

      Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitma ...

  7. Android性能优化典范第一季

    2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...

  8. [转]Android性能优化典范

    2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...

  9. [Android Pro] Android性能优化典范第一季

    reference to : http://www.cnblogs.com/hanyonglu/p/4244035.html#undefined 2015年伊始,Google发布了关于Android性 ...

随机推荐

  1. CF901C. Bipartite Segments

    n<=300000,m<=300000的图,图上只有奇环,q<=300000个询问每次问:一个区间内有多少个子区间,满足只保留编号在该区间的点以及他们之间的边,可以构成一个二分图. ...

  2. [bzoj3894]文理分科_网络流_最小割

    文理分科 bzoj-3894 题目大意:题目链接. 注释:略. 想法: 这种题也是一种套路. 我们新建一个点表示收益点. 然后把所有的收益都加一起,求最小割表示代价即可. Code: #include ...

  3. iis站点内存泄漏问题分析

    在一次上线过程中iis内存飙升,随后跟运维要到站点的dump文件,使用windbg分析了clr的内存分配,找到了问题的症结,先记录如下: 使用windbg加载dump文件 1.打开windbg,Fil ...

  4. #!/usr/bin/env 脚本解释程序的作用

    the Zimbu programming language http://www.zimbu.org/getting-started -------------------------------- ...

  5. day4-hdfs的核心工作原理\写数据流程 \读数据流程

    namenode元数据管理要点 1.什么是元数据? hdfs的目录结构及每一个文件的块信息(块的id,块的副本数量,块的存放位置<datanode>) 2.元数据由谁负责管理? namen ...

  6. Java String常见问题

    一.怎样推断两个String是否相等??使用"=="还是使用"equals()"? 对String来说."=="是用来推断两个字符串(对象) ...

  7. 使用逆向工程生成mybatis的Mapper文件

    之前有写过一篇博客: 使用MyBatis Generator自动生成MyBatis的代码链接:http://www.cnblogs.com/klslb/p/6908535.html 这个太麻烦了,而且 ...

  8. Node.js+Express搭建博客系统基本环境安装

    1.下载安装node.js 官网下载地址:https://nodejs.org/en/download/ 2.安装express. 打开node命令行工具,在命令行中输入:npm install -g ...

  9. Nginx入门详解文档

    1 文章内容 掌握nginx+tomcat反向代理的使用方法. 掌握nginx作为负载均衡器的使用方法. 掌握nginx实现web缓存方法. 2 nginx介绍 2.1 什么是nginx Nginx是 ...

  10. Joseph问题 (线段树)

    Joseph问题似乎是入门题,就是那个报数出圈的问题,不过它暴力模拟的复杂度是O(nm)的,如果题目的数据范围达到了30000,那就超时了.怎么用线段树维护呢? 我们可以这么考虑,每次我们其实要查询在 ...