源码版本:Android11-release
直接adb shell执行settings put secure clock_seconds 1可以直接看到效果(当次生效,如果之前写过这个settings值就先写0再写1让他刷新)
源码分析:
系统原生实现了时钟显示秒,不过开关入口在系统界面调谐器里,一般定制系统都会屏蔽这个入口,功能实现在Clock.java内:
//frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\policy\Clock.java
//69行
public static final String CLOCK_SECONDS = "clock_seconds";
//295行
@Override
public void onTuningChanged(String key, String newValue) {
if (CLOCK_SECONDS.equals(key)) {//监听settings值更改,读值判断是否需要显示秒
mShowSeconds = TunerService.parseIntegerSwitch(newValue, false);
updateShowSeconds();
} else {
setClockVisibleByUser(!StatusBarIconController.getIconBlacklist(getContext(), newValue)
.contains("clock"));
updateClockVisibility();
}
}
//frameworks\base\packages\SystemUI\src\com\android\systemui\tuner\TunerServiceImpl.java
//219行
private void reloadSetting(Uri uri) {
String key = mListeningUris.get(uri);
Set<Tunable> tunables = mTunableLookup.get(key);
if (tunables == null) {
return;
}
String value = Settings.Secure.getStringForUser(mContentResolver, key, mCurrentUser);
for (Tunable tunable : tunables) {
tunable.onTuningChanged(key, value);
}
}
改settings值后触发TunerServiceImpl内reloadSetting方法,会回调Clock 的onTuningChanged方法,切换显示时分秒/时分模式