共计 5197 个字符,预计需要花费 13 分钟才能阅读完成。
和您一起一生学习,这里是程序员 Android
本篇文章次要介绍 Android 开发中的局部知识点,通过浏览本篇文章,您将播种以下内容:
一、Doze 模式
二、闲暇状态下,优化 app 耗电
三、Doze 模式下的限度措施
四、Doze 模式概要
五、Doze 模式波及的类如下:
六、Doze 模式状态
七、Doze 白名单
八、Doze 模式测试方法
九、开启 Doze dubug 调试开关
# 一、Doze 模式
当设施处于非充电、灭屏状态下静止一段时间,设施将进入睡眠状态,进入 Doze
模式,缩短电池应用工夫。Doze
模式下零碎会定期恢复正常操作,异步执行 ap
p 的一些同步数据等操作。比方很长时间不应用,零碎会容许设施一天拜访一次网络等。当设施处于充电状态下,零碎将进入规范模式,app
执行操作将不被限度。
二、闲暇状态下,优化 app 耗电
在用户没有应用 app
的状况下,零碎会使 app
处于 idle
状态,
在闲暇状态下,零碎将会禁止 app
网络拜访以及数据同步
三、Doze 模式下的限度措施
1. 禁止网络拜访
2. 疏忽Wake lock
3. 疏忽Alarms(setAlarmClock()、AlarmManager.setAndAllowwhileIdle()
这两个办法除外)
4. 疏忽 WIFI
扫描
5. 同步作业调度程序将不被执行
四、Doze 模式概要
五、Doze 模式波及的类如下:
frameworks/base/services/core/java/com/android/server/DeviceIdleController.java
/**
* Keeps track of device idleness and drives low power mode based on that.
*/
public class DeviceIdleController extends SystemService
implements AnyMotionDetector.DeviceIdleCallback {
六、Doze 模式状态
- ACTIVE:手机设施处于激活活动状态
- INACTIVE:屏幕敞开进入非活动状态
- IDLE_PENDING:每隔 30 分钟让 App 进入期待闲暇准备状态
- IDLE:闲暇状态
- IDLE_MAINTENANCE:解决挂起工作
对应的 Doze 模式状态如下:
active—> inactive —> idle_pending
静止模式检测
void handleMotionDetectedLocked(long timeout, String type) {
// The device is not yet active, so we want to go back to the pending idle
// state to wait again for no motion. Note that we only monitor for motion
// after moving out of the inactive state, so no need to worry about that.
boolean becomeInactive = false;
if (mState != STATE_ACTIVE) {scheduleReportActiveLocked(type, Process.myUid());
mState = STATE_ACTIVE;
mInactiveTimeout = timeout;
mCurIdleBudget = 0;
mMaintenanceStartTime = 0;
EventLogTags.writeDeviceIdle(mState, type);
addEvent(EVENT_NORMAL);
becomeInactive = true;
}
if (mLightState == LIGHT_STATE_OVERRIDE) {
// We went out of light idle mode because we had started deep idle mode... let's
// now go back and reset things so we resume light idling if appropriate.
mLightState = STATE_ACTIVE;
EventLogTags.writeDeviceIdleLight(mLightState, type);
becomeInactive = true;
}
if (becomeInactive) {becomeInactiveIfAppropriateLocked();
}
}
idle_pending ————>sensing
@Override
public void onAnyMotionResult(int result) {if (DEBUG) Slog.d(TAG, "onAnyMotionResult(" + result + ")");
if (result != AnyMotionDetector.RESULT_UNKNOWN) {synchronized (this) {cancelSensingTimeoutAlarmLocked();
}
}
if (result == AnyMotionDetector.RESULT_MOVED) {if (DEBUG) Slog.d(TAG, "RESULT_MOVED received.");
synchronized (this) {handleMotionDetectedLocked(mConstants.INACTIVE_TIMEOUT, "sense_motion");
}
} else if (result == AnyMotionDetector.RESULT_STATIONARY) {if (DEBUG) Slog.d(TAG, "RESULT_STATIONARY received.");
if (mState == STATE_SENSING) {
// If we are currently sensing, it is time to move to locating.
synchronized (this) {
mNotMoving = true;
stepIdleStateLocked("s:stationary");
}
} else if (mState == STATE_LOCATING) {
// If we are currently locating, note that we are not moving and step
// if we have located the position.
synchronized (this) {
mNotMoving = true;
if (mLocated) {stepIdleStateLocked("s:stationary");
}
}
}
}
}
七、Doze 白名单
电量优化白名单
设置 – 电池 – 电量优化(menu 菜单)
会设置查看 app 电池优化应用状况
白名单是以 xml 模式存储 (deviceidle.xml)
查看白名单命令
// 次要寄存 app 包名
adb shell dumpsys deviceidle whitelist
白名单代码保留局部代码如下
/**
* Package names the system has white-listed to opt out of power save restrictions,
* except for device idle mode.
*/
private final ArrayMap<String, Integer> mPowerSaveWhitelistAppsExceptIdle = new ArrayMap<>();
/**
* Package names the system has white-listed to opt out of power save restrictions for
* all modes.
*/
private final ArrayMap<String, Integer> mPowerSaveWhitelistApps = new ArrayMap<>();
/**
* Package names the user has white-listed to opt out of power save restrictions.
*/
private final ArrayMap<String, Integer> mPowerSaveWhitelistUserApps = new ArrayMap<>();
/**
* App IDs of built-in system apps that have been white-listed except for idle modes.
*/
private final SparseBooleanArray mPowerSaveWhitelistSystemAppIdsExceptIdle
= new SparseBooleanArray();
/**
* App IDs of built-in system apps that have been white-listed.
*/
private final SparseBooleanArray mPowerSaveWhitelistSystemAppIds = new SparseBooleanArray();
/**
* App IDs that have been white-listed to opt out of power save restrictions, except
* for device idle modes.
*/
private final SparseBooleanArray mPowerSaveWhitelistExceptIdleAppIds = new SparseBooleanArray();
/**
* Current app IDs that are in the complete power save white list, but shouldn't be
* excluded from idle modes. This array can be shared with others because it will not be
* modified once set.
*/
private int[] mPowerSaveWhitelistExceptIdleAppIdArray = new int[0];
/**
* App IDs that have been white-listed to opt out of power save restrictions.
*/
private final SparseBooleanArray mPowerSaveWhitelistAllAppIds = new SparseBooleanArray();
/**
* Current app IDs that are in the complete power save white list. This array can
* be shared with others because it will not be modified once set.
*/
private int[] mPowerSaveWhitelistAllAppIdArray = new int[0];
/**
* App IDs that have been white-listed by the user to opt out of power save restrictions.
*/
private final SparseBooleanArray mPowerSaveWhitelistUserAppIds = new SparseBooleanArray();
/**
* Current app IDs that are in the user power save white list. This array can
* be shared with others because it will not be modified once set.
*/
private int[] mPowerSaveWhitelistUserAppIdArray = new int[0];
八、Doze 模式测试方法
1. 开启 Doze
`
adb shell dumpsys deviceidle enable`
`// or MTK addadb shell setprop persist.config.AutoPowerModes 1
`
2. 拔掉电池
`
adb shell dumpsys battery unplug
`
3. 调试 Doze 状态
Active ---idle_pending---sensing--location---idle --idle_mantenance
`
adb shell dumpsys deviceidle step
`
4.Dump Doze 状态剖析
Doze
模式下的信息,包含电池电量优化白名单等
`
adb shell dumpsys deviceidle
`
九、开启 Doze dubug 调试开关
默认false
敞开,设置为true
开启DeviceIdleController.java
private static final boolean DEBUG = false;
情谊举荐:
Android 干货分享
至此,本篇已完结。转载网络的文章,小编感觉很优良,欢送点击浏览原文,反对原创作者,如有侵权,恳请分割小编删除,欢迎您的倡议与斧正。同时期待您的关注,感谢您的浏览,谢谢!