前言
Andorid 代码里如何设置永不休眠时间,以及Settings 中增加超时时间选择
应用如何设置休眠时间
- 应用需要
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
权限 - 通过如下代码设置屏幕超时时间,注意传入的time 时间单位是毫秒
Settings.System.putInt(getContentResolver(),android.provider.Settings.System.SCREEN_OFF_TIMEOUT, time );
Settings 中如何设置的休眠时间?
Settings 是通过该TimeoutPreferenceController.java 控制的
packages/apps/Settings/src/com/android/settings/display/TimeoutPreferenceController.java
@Override
public void updateState(Preference preference) {
final TimeoutListPreference timeoutListPreference = (TimeoutListPreference) preference;
final long currentTimeout = Settings.System.getLong(mContext.getContentResolver(),
SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE);
//<!–– add by lixh0815 for task299 20190418 begin
if(isZh(mContext)){
//代码1
timeoutListPreference.setEntries(R.array.cn_screen_timeout_entries);
timeoutListPreference.setEntryValues(R.array.cn_screen_timeout_values);
}
//代码2
long timeoutValue = (currentTimeout == Integer.MAX_VALUE) ? -1 : currentTimeout;
if(timeoutValue == -1 && !(isZh(mContext))){
timeoutValue = FALLBACK_SCREEN_TIMEOUT_VALUE;
Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE);
}
timeoutListPreference.setValue(String.valueOf(timeoutValue));
// add by lixh0815 for task299 20190418 end ––>
final DevicePolicyManager dpm =
(DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
if (dpm != null) {
final RestrictedLockUtils.EnforcedAdmin admin =
RestrictedLockUtils.checkIfMaximumTimeToLockIsSet(mContext);
final long maxTimeout =
dpm.getMaximumTimeToLock(null /* admin */, UserHandle.myUserId());
timeoutListPreference.removeUnusableTimeouts(maxTimeout, admin);
}
updateTimeoutPreferenceDescription(timeoutListPreference, timeoutValue);
EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(
mContext, UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT,
UserHandle.myUserId());
if(admin != null) {
timeoutListPreference.removeUnusableTimeouts(0/* disable all*/, admin);
} else {
MdmDisplayManager displayManager = MdmDisplayManager.get(mContext);
if(displayManager != null) {
try {
timeoutListPreference.setEnabled(displayManager.isChangeScreenTimeoutAllowed());
} catch(RuntimeException e) {}
}
}
}
//<!–– update by lixh0815 for task299 20190418 begin
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
try {
int timeoutValue = Integer.parseInt((String) newValue);
//代码3
int value = ( -1 == timeoutValue) ? Integer.MAX_VALUE : timeoutValue;
Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, value);
updateTimeoutPreferenceDescription((TimeoutListPreference) preference, timeoutValue);
} catch (NumberFormatException e) {
Log.e(TAG, "could not persist screen timeout setting", e);
}
return true;
}
`packages/apps/Settings/res/values-zh-rTW/arrays.xml`
```xml
<string-array name="cn_screen_timeout_entries">
<item>"永不"</item>
<item msgid="3342301044271143016">"15 秒"</item>
<item msgid="8881760709354815449">"30 秒"</item>
<item msgid="7589406073232279088">"1 分鐘"</item>
<item msgid="7001195990902244174">"2 分鐘"</item>
<item msgid="7489864775127957179">"5 分鐘"</item>
<item msgid="2314124409517439288">"10 分鐘"</item>
<item msgid="6864027152847611413">"30 分鐘"</item>
</string-array>
packages/apps/Settings/res/values/arrays.xml
<string-array name="cn_screen_timeout_values" translatable="false">
<item>-1</item>
<!-- Do not translate. -->
<item>15000</item>
<!-- Do not translate. -->
<item>30000</item>
<!-- Do not translate. -->
<item>60000</item>
<!-- Do not translate. -->
<item>120000</item>
<!-- Do not translate. -->
<item>300000</item>
<!-- Do not translate. -->
<item>600000</item>
<!-- Do not translate. -->
<item>1800000</item>
</string-array>
Settings 中显示的选项从 cn_screen_timeout_entries
中读取,对应值传入。从代码3可以看出在Settings
应用中,值的单位是 毫秒,当选择永不
休眠时传入 -1
值,而这个 -1
值在真正的传入 Settings.System.putInt
中时就被修改为了Integer.MAX_VALUE
;
如果要增加Settings
休眠时间可选要,则只要修改cn_screen_timeout_values
、cn_screen_timeout_entries
这两个string-array
就行了。
应用中设置永不休眠
所以,如果应用要设置当前系统永不休眠,只要如下设置就行了
Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_OFF_TIMEOUT, Integer.MAX_VALUE );