场景:
省电APP监听电池电量变化,然后动态刷新UI!
我们平时最经常使用的是sendBroadcast,就是把一个Intent广播出去。
/**
* Perform a {@link #sendBroadcast(Intent)} that is "sticky," meaning the
* Intent you are sending stays around after the broadcast is complete,
* so that others can quickly retrieve that data through the return
* value of {@link #registerReceiver(BroadcastReceiver, IntentFilter)}. In
* all other ways, this behaves the same as
*
*/
@Deprecated
public abstract void sendStickyBroadcast(Intent intent);
这类广播的表现形式就是,发出去的Intent会一直停留保持,直到broadcast事件结束,其它对象能够快速的取回数据,通过registerReceiver注册时候返回值!
通俗点,广播会一直滞留(等待),以便有人注册这则广播消息后能尽快的收到这条广播
这种广播也没有所谓的10秒限制,10秒限制是指普通的广播如果onReceive方法执行时间太长,超过10秒的时候系统会将这个广播置为可以干掉的candidate,一旦系统资源不够的时候,就会干掉这个广播而让它不执行。
回到今天的场景:
BatteryManager 会发送“sticky”类型的系统广播,在 Intent 中包括了当前的电池电量状态和充电状态等信息。因为电池状态的广播类型是 “sticky”类型的,所以我们不需要注册相应的BroadcastReceiver。只需要在调用 registerReceiver 的时候传递空参数null就可以,然后函数的返回值 intent 中就包括了当前电池状态的各种信息。
我们有BatteryInfo这样一个类,她是一个单例,每次刷新UI的时候,都从这个单例里面获取电池信息:
private void update() {
Intent intent = this.context.registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
if (intent == null) {
Log.w("BatteryInfo", "get current battery state error, intent is null");
} else {
updateByIntent(intent);
}
}
public BatteryInfo updateByIntent(Intent intent) {
this.currentBatteryStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);//充电,充满,未充电,未知
this.currentBatteryPlugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);//当前手机使用的是哪里的电源
this.currentBatteryTemperature = (((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1))) / 10f;
this.currentBatteryHealth = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);//电池的健康状态
this.currentBatteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);//电池当前的电量, 它介于0和EXTRA_SCALE之间
this.currentBatteryVoltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);//当前电池的电压
this.batteryScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);//电池电量的最大值
this.batteryTechnology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);//"technology"
this.batteryEnergy = (((float) this.currentBatteryLevel)) / (((float) this.batteryScale));
this.batteryVoltage = ((float) this.currentBatteryVoltage);
if (this.batteryEnergy > 1f) {
this.batteryEnergy = 1f;
}
this.currentBatteryEnergy = ((int) (this.batteryEnergy * 100f));
if (this.batteryVoltage > 100f) {
this.batteryVoltage /= 1000f;
}
return this;
}
通过监听"android.intent.action.BATTERY_CHANGED"这个粘性广播,我们能够快速获取最后的一个Intent数据,从而解析出最新的电池相关数据!
PS一下:因为安全考虑,app主动sendStickyOrderedBroadcast和sendStickyBroadcast已经被弃用了~~~
@deprecated Sticky broadcasts should not be used. They provide no security (anyone
* can access them), no protection (anyone can modify them), and many other problems.
* The recommended pattern is to use a non-sticky broadcast to report that something
* has changed, with another mechanism for apps to retrieve the current value whenever
* desired.
最后提一下:
sendBroadcast的三种发送方法:sendBroadcast(), sendOrderedBroadcast() 和 ** sendStickyBroadcast()**