电池详情获取应用运行时间

news/2024/11/15 6:58:37/

1. 电池用量时间一直为0

电池

2. 源码跟踪

2.1 字符串位置

    <string name="battery_detail_foreground" msgid="3350401514602032183">"在前台运行时"</string><string name="battery_detail_background" msgid="1929644393553768999">"在后台运行时"</string>

2.2 布局文件

power_usage_detail.xml

    <PreferenceCategoryandroid:title="@string/battery_detail_info_title"><Preference// 前台进程运行android:key="app_usage_foreground"android:title="@string/battery_detail_foreground"android:selectable="false"/><Preference// 后台进程运行android:key="app_usage_background"android:title="@string/battery_detail_background"android:selectable="false"/><Preference// 电池耗电量android:key="app_power_usage"android:title="@string/battery_detail_power_usage"android:selectable="false"/></PreferenceCategory>

2.3 控件定义

package com.android.settings.fuelgauge;/*** Power usage detail fragment for each app, this fragment contains** 1. Detail battery usage information for app(i.e. usage time, usage amount)* 2. Battery related controls for app(i.e uninstall, force stop)*/
public class AdvancedPowerUsageDetail extends DashboardFragment implementsButtonActionDialogFragment.AppButtonsDialogListener,AnomalyDialogFragment.AnomalyDialogListener,LoaderManager.LoaderCallbacks<List<Anomaly>> {private static final String KEY_PREF_FOREGROUND = "app_usage_foreground";private static final String KEY_PREF_BACKGROUND = "app_usage_background";private static final String KEY_PREF_POWER_USAGE = "app_power_usage";public void onCreate(Bundle icicle) {super.onCreate(icicle);mForegroundPreference = findPreference(KEY_PREF_FOREGROUND);mBackgroundPreference = findPreference(KEY_PREF_BACKGROUND);mPowerUsagePreference = findPreference(KEY_PREF_POWER_USAGE);

2.4 控件赋值

    @VisibleForTestingvoid initPreference() {final Bundle bundle = getArguments();final Context context = getContext();final long foregroundTimeMs = bundle.getLong(EXTRA_FOREGROUND_TIME);final long backgroundTimeMs = bundle.getLong(EXTRA_BACKGROUND_TIME);final String usagePercent = bundle.getString(EXTRA_POWER_USAGE_PERCENT);final int powerMah = bundle.getInt(EXTRA_POWER_USAGE_AMOUNT);mForegroundPreference.setSummary(TextUtils.expandTemplate(getText(R.string.battery_used_for),Utils.formatElapsedTime(context, foregroundTimeMs, false)));mBackgroundPreference.setSummary(TextUtils.expandTemplate(getText(R.string.battery_active_for),Utils.formatElapsedTime(context, backgroundTimeMs, false)));mPowerUsagePreference.setSummary(getString(R.string.battery_detail_power_percentage, usagePercent, powerMah));}

2.5 运行时间获取 startBatteryDetailPage.args.putLong

package com.android.settings.fuelgauge;/*** Power usage detail fragment for each app, this fragment contains** 1. Detail battery usage information for app(i.e. usage time, usage amount)* 2. Battery related controls for app(i.e uninstall, force stop)*/
public class AdvancedPowerUsageDetail extends DashboardFragment implementsButtonActionDialogFragment.AppButtonsDialogListener,AnomalyDialogFragment.AnomalyDialogListener,LoaderManager.LoaderCallbacks<List<Anomaly>> {@VisibleForTestingstatic void startBatteryDetailPage(SettingsActivity caller, BatteryUtils batteryUtils,PreferenceFragment fragment, BatteryStatsHelper helper, int which, BatteryEntry entry,String usagePercent, List<Anomaly> anomalies) {......final Bundle args = new Bundle();final BatterySipper sipper = entry.sipper;final BatteryStats.Uid uid = sipper.uidObj;final boolean isTypeApp = sipper.drainType == BatterySipper.DrainType.APP;final long foregroundTimeMs = isTypeApp ? batteryUtils.getProcessTimeMs(BatteryUtils.StatusType.FOREGROUND, uid, which) : sipper.usageTimeMs;final long backgroundTimeMs = isTypeApp ? batteryUtils.getProcessTimeMs(BatteryUtils.StatusType.BACKGROUND, uid, which) : 0;......args.putInt(EXTRA_UID, sipper.getUid());args.putLong(EXTRA_BACKGROUND_TIME, backgroundTimeMs);args.putLong(EXTRA_FOREGROUND_TIME, foregroundTimeMs);args.putString(EXTRA_POWER_USAGE_PERCENT, usagePercent);args.putInt(EXTRA_POWER_USAGE_AMOUNT, (int) sipper.totalPowerMah);args.putParcelableList(EXTRA_ANOMALY_LIST, anomalies);

2.6 运行时间获取 batteryUtils.getProcessTimeMs

package com.android.settings.fuelgauge;/*** Utils for battery operation*/
public class BatteryUtils {public long getProcessTimeMs(@StatusType int type, @Nullable BatteryStats.Uid uid,int which) {if (uid == null) {return 0;}switch (type) {case StatusType.SCREEN_USAGE:return getScreenUsageTimeMs(uid, which);case StatusType.FOREGROUND:return getProcessForegroundTimeMs(uid, which);case StatusType.BACKGROUND:return getProcessBackgroundTimeMs(uid, which);case StatusType.ALL:return getProcessForegroundTimeMs(uid, which)+ getProcessBackgroundTimeMs(uid, which);}return 0;}

2.6 运行时间获取 BatteryUtils.getProcessForegroundTimeMs

adb shell logcat -s “BatteryUtils”

private static final String TAG = "BatteryUtils";private long getProcessForegroundTimeMs(BatteryStats.Uid uid, int which) {final long rawRealTimeUs = convertMsToUs(SystemClock.elapsedRealtime());return getScreenUsageTimeMs(uid, which, rawRealTimeUs)+ convertUsToMs(getForegroundServiceTotalTimeUs(uid, rawRealTimeUs));}// 屏幕使用时间private long getScreenUsageTimeMs(BatteryStats.Uid uid, int which, long rawRealTimeUs) {final int foregroundTypes[] = {BatteryStats.Uid.PROCESS_STATE_TOP};Log.v(TAG, "package: " + mPackageManager.getNameForUid(uid.getUid()));long timeUs = 0;for (int type : foregroundTypes) {final long localTime = uid.getProcessStateTime(type, rawRealTimeUs, which);Log.v(TAG, "type: " + type + " time(us): " + localTime);timeUs += localTime;}Log.v(TAG, "foreground time(us): " + timeUs);// Return the min value of STATE_TOP time and foreground activity time, since both of these// time have some errorsreturn convertUsToMs(Math.min(timeUs, getForegroundActivityTotalTimeUs(uid, rawRealTimeUs)));}// 进程前台运行时间@VisibleForTestinglong getForegroundServiceTotalTimeUs(BatteryStats.Uid uid, long rawRealtimeUs) {final BatteryStats.Timer timer = uid.getForegroundServiceTimer();if (timer != null) {return timer.getTotalTimeLocked(rawRealtimeUs, BatteryStats.STATS_SINCE_CHARGED);}return 0;}

http://www.ppmy.cn/news/240200.html

相关文章

如何维持手机电池寿命_手机电池寿命是多久? 如何延长手机电池寿命?

一、手机电池寿命是多久 一般锂电池的使用寿命是500-1000个完整充放电轮次,同时一般从第50-100左右的时候开始电量逐渐衰减。折算成我们平时使用的情况,大概就是使用寿命在3年到5年之间。 二、如何延长手机电池寿命 1、避免让手机长时间处于高温的环境中。很多人都有边充电边…

蓝牙芯片排行_2020年4月TWS触摸芯片出货量排行榜

&#xfeff; 提示&#xff1a;点击上方"蓝字"↑免费订阅本公众号 今年4月&#xff0c;TWS触摸芯片总出货量为49.4kk&#xff0c;环比3月下降32.8%。相比3月&#xff0c;疫情的影响在4月影响更是严重。 从榜单上看&#xff0c;受影响最大的是排名前二的企业。其中&am…

android 金山电池医生,金山电池医生3.0(android版).PDF

金山电池医生说明文档V3.0 (For Android ) 金山电池医生说明文档 V3.0 (For Android) 目录 安装 2 启动 4 主界面及省电优化 6 主界面—充电 13 省电模式 18 耗电排行 23 精品 23 设置 25 桌面小部件 26 menu 菜单 27 卸载 29 金山电池医生说明文档V3.0 (For Android ) 安装 1&…

蓝牙芯片排行_8月TWS硅麦出货量排行榜TOP15

数据铸造影响力 目前&#xff0c;MEMS麦克风的主要应用领域在3C电子产品(计算机、通讯类设备、消费电子产品)、医疗电子、汽车电子、可穿戴设备、物联网等领域。其中&#xff0c;3C电子产品领域是目前我国MEMS麦克风最大应用领域之一。 撰文 / 旭日大数据 编辑 / 风之雅韵…

蓝牙芯片排行_7月TWS硅麦出货量排行榜出炉

数据铸造影响 撰文 / 旭日大数据 编辑 / 风之雅韵 行业背景 硅麦是TWS耳机的要件之一&#xff0c;也是今年TWS耳机热点之一——ANC降噪——的标配。 一般地&#xff0c;一部带ANC降噪耳机会配置4颗硅麦&#xff0c;两颗采集用户的声音&#xff0c;两颗采集环境噪音&#xf…

蓝牙芯片排行_7月TWS 全球品牌出货量排行榜出炉

数据铸造影响力 撰文 / 旭日大数据 编辑 / 柏序 旭日大数据公布了2020年7月全球TWS品牌销量排行榜&#xff0c;与上期数据相比&#xff0c;全球品牌七排名TOP20汰换率为15%&#xff0c;其中DOSS&#xff0c;万魔、BOSE跌出前20&#xff0c;广州由我&#xff0c;Tzumi登榜&a…

蓝牙芯片排行_七月蓝牙芯片出货量排行榜TOP10

数据铸造影响力 撰文 / 旭日大数据 编辑 / 柏序 7月TWS蓝牙芯片出货量总体呈上升态势&#xff0c;疫情对制造业的影响开始淡化&#xff0c;行业内对TWS八、九月份的市场信心增强&#xff0c;因此备货量增多。白牌市场与品牌市场的角逐拉开帷幕。 蓝牙芯片前五强总体出货量为…

如何维持手机电池寿命_手机电池寿命怎么延长

一、避免让手机长时间处于高温的环境中 很多人都有边充电边玩手机的习惯&#xff0c;充电时使用手机会使手机电池发烫&#xff0c;不仅影响使用体验&#xff0c;还会影响电池的寿命&#xff0c;甚至还可能引起燃烧或爆炸&#xff1b;另外不要将手机留在阳光曝晒下的车里&#x…