Android5.1 电池充电剩余时间计算

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Android5.1 电池充电剩余时间计算android5.1手机在充电的时候,并且在锁屏界面的时候会显示还剩多少时间电池充满电。我们就这个机制进行下深入分析:

首先对电池的变化都会监听BatteryService发出的Intent.ACTION_BATTERY_CHANGED广播,因此在framework目录下全局搜索,结果发现在./base/packages/Keyguard/src/com/Android/keyguard/KeyguardUpdateMonitor.Java这个目录下,也就是keyguard中有对这个广播的监控

在KeyguardUpdateMonitor.java这个文件中

[java] view plain copy

private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

final String action = intent.getAction();

if (DEBUG) Log.d(TAG, "received broadcast " + action);

if (Intent.ACTION_TIME_TICK.equals(action)

|| Intent.ACTION_TIME_CHANGED.equals(action)

|| Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {

mHandler.sendEmptyMessage(MSG_TIME_UPDA TE);

} else if (Intent.ACTION_BA TTERY_CHANGED.equals(action)) {//监听电池变化的广播

final int status = intent.getIntExtra(EXTRA_STA TUS, BA TTERY_STATUS_UNKNOWN);

final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0);

final int level = intent.getIntExtra(EXTRA_LEVEL, 0);

final int health = intent.getIntExtra(EXTRA_HEALTH, BA TTERY_HEALTH_UNKNOWN);

final Message msg = mHandler.obtainMessage(

MSG_BA TTERY_UPDATE, new BatteryStatus(status, level, plugged, health));

mHandler.sendMessage(msg);//发消息

}

接下来我们搜索下MSG_BATTERY_UPDATE这个消息,

[java] view plain copy

private void handleBatteryUpdate(BatteryStatus status) {

if (DEBUG) Log.d(TAG, "handleBatteryUpdate");

final boolean batteryUpdateInteresting = isBatteryUpdateInteresting(mBatteryStatus, status);

mBatteryStatus = status;

if (batteryUpdateInteresting) {

for (int i = 0; i < mCallbacks.size(); i++) {

KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();

if (cb != null) {

cb.onRefreshBatteryInfo(status);//接下来我们就搜一下谁定义了onRefreshBatteryInfo这个接口

}

}

}

}

结果在KeyguardIndicationController.java这个文件中

[java] view plain copy

KeyguardUpdateMonitorCallback mUpdateMonitor = new KeyguardUpdateMonitorCallback() { @Override

public void onRefreshBatteryInfo(KeyguardUpdateMonitor.BatteryStatus status) { boolean isChargingOrFull = status.status == BatteryManager.BATTERY_STA TUS_CHARGING

|| status.status == BatteryManager.BA TTERY_STATUS_FULL;

mPowerPluggedIn = status.isPluggedIn() && isChargingOrFull;

mPowerCharged = status.isCharged();

updateIndication();

}

};

接下来看下updateIndication

[java] view plain copy

private void updateIndication() {

if (mVisible) {

mTextView.switchIndication(computeIndication());

}

}

接下来就是看下如何计算电池剩余时间的

[java] view plain copy

private String computeIndication() {

if (!TextUtils.isEmpty(mTransientIndication)) {

return mTransientIndication;

}

if (mPowerPluggedIn) {//当在充电时

return computePowerIndication();

}

return mRestingIndication;

}

private String computePowerIndication() {

if (mPowerCharged) {

return mContext.getResources().getString(R.string.keyguard_charged);

}

// Try fetching charging time from battery stats.

相关文档
最新文档