Android 性能优化(12)网络优化( 8)Monitoring the Battery Level and Charging State
This lesson teaches you to
- Determine the Current Charging State
- Monitor Changes in Charging State
- Determine the Current Battery Level
- Monitor Significant Changes in Battery Level
You should also read
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.
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
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:
// Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL; // How are we charging?
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
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_CONNECTED
and ACTION_POWER_DISCONNECTED
within an intent filter.
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
Within the associated BroadcastReceiver
implementation, you can extract the current charging state and method as described in the previous step.
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL; int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
}
}
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:
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -); 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
.
<receiver android:name=".BatteryLevelReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
<action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>
</intent-filter>
</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的更多相关文章
- Android性能优化典范(二)
Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...
- android app性能优化大汇总(google官方Android性能优化典范 - 第2季)
Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...
- Android性能优化典范 - 第2季
Google发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的缩放,缓 ...
- Android 性能优化探究
使用ViewStub动态载入布局.避免一些不常常的视图长期握住引用: ViewStub的一些特点: 1. ViewStub仅仅能Inflate一次,之后ViewStub对象被置空:某个被ViewStu ...
- android 性能优化
本章介绍android高级开发中,对于性能方面的处理.主要包括电量,视图,内存三个性能方面的知识点. 1.视图性能 (1)Overdraw简介 Overdraw就是过度绘制,是指在一帧的时间内(16. ...
- Android性能优化典范第二季
Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitma ...
- Android性能优化典范第一季
2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...
- [转]Android性能优化典范
2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...
- [Android Pro] Android性能优化典范第一季
reference to : http://www.cnblogs.com/hanyonglu/p/4244035.html#undefined 2015年伊始,Google发布了关于Android性 ...
随机推荐
- 【HDOJ4812】D Tree(点分治)
题意: 给定一棵 n 个点的树,每个点有权值 Vi 问是否存在一条路径使得路径上所有点的权值乘积 mod(10^6 + 3) 为 K 输出路径的首尾标号,若有多解,输出字典序最小的解 对于100%的数 ...
- key通用操作【九】
一.概述: 在该系列的前几篇博客中,主要讲述的是与Redis数据类型相关的命令,如String.List.Set.Hashes和Sorted-Set.这些命令都具有一个共同点,即所有的操作都是针对与K ...
- 【进击后端】Ubuntu 命令行 安装nginx
一.安装nginx apt-get install nginx 安装路径为:/etc/nginx/conf.d 二.配置nginx,在conf.d目录下新建test.conf 新建文件的命令是vi t ...
- dubbo的jmeter压测时jar包的热加载/动态加载
在做dubbo的jmeter压测时,需要把jar包放入jmeter的lib/ext目录下,但是jmeter启动的时候会自动加载这个目录lib目录及lib/ext目录,这样启动后放入这些目录下的jar包 ...
- Mysql入门实战中
前面一章主要解说了mysql的入门学习.包括数据库,表的管理,以及对数据的增删改,本章主要介绍mysql最重要的语句select的使用方法.将select的大部分使用方法进行分别解说. 全部代码下载( ...
- phpunit 单元测试框架-代码覆盖率
"phpize not found" 的解决办法: apt-get install php5-dev http://jeffreysambells.com/2010/04/08/r ...
- 【转】深入理解javascript作用域——词法作用域和动态作用域
前面的话 大多数时候,我们对作用域产生混乱的主要原因是分不清楚应该按照函数位置的嵌套顺序,还是按照函数的调用顺序进行变量查找.再加上this机制的干扰,使得变量查找极易出错.这实际上是由两种作用域工作 ...
- git fetch 和 git pull 的差别
Git中从远程的分支获取最新的版本号到本地有这样2个命令: 1. git fetch:相当于是从远程获取最新版本号到本地,不会自己主动merge git fetch origin master git ...
- UML中的四种关系总结
UML中的关系主要包含四种:关联关系.依赖关系.泛化关系.实现关系.当中关联关系还包含聚合关系和组合关系. 1. 关联关系(Association) 关联关系式一种结构化的关系,是指一种对象和还有一种 ...
- 静态链表的C++实现
静态链表是使用数组实现的能够高速插入和删除数据的链表,静态链表和链式单链表比的缺点在于链表的长度仅仅能初始化设置好,而相相应普通的顺序存储的链表,静态链表不能实现高速的读写随意的元素. 当然静态链表给 ...