桌面的数字时间的widget的显示主要需要关注的有如下5个。
vendor\mediatek\proprietary\packages\apps\DeskClock\src\com\android\alarmclock\DigitalAppWidgetProvider.java
vendor\mediatek\proprietary\packages\apps\DeskClock\res\layout\digital_widget.xml
vendor\mediatek\proprietary\packages\apps\DeskClock\res\values\donottranslate.xml
vendor\mediatek\proprietary\packages\apps\MtkSettings\src\com\android\settings\datetime\TimeFormatPreferenceController.java
frameworks\base\core\java\android\widget\TextClock.java
系统设置界面中的 使用24小时制 开关,点击后时间会立马改变显示,就是通过发送 ACTION_TIME_CHANGED 广播。
出处:https://www.cnblogs.com/cczheng-666/p/10958920.html
//vendor\mediatek\proprietary\packages\apps\MtkSettings\src\com\android\settings\datetime\TimeFormatPreferenceController.java
private void set24Hour(boolean is24Hour) {
Settings.System.putString(mContext.getContentResolver(),
Settings.System.TIME_12_24,
is24Hour ? HOURS_24 : HOURS_12);
}
private void timeUpdated(boolean is24Hour) {
Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED);
int timeFormatPreference =
is24Hour ? Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR
: Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR;
timeChanged.putExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, timeFormatPreference);
mContext.sendBroadcast(timeChanged);
}
由于点击setting中的时间格式切换后出现如下log。
W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.TIME_SET flg=0x10 (has extras) } to com.android.deskclock/com.android.alarmclock.DigitalAppWidgetProvider
根据这篇文章可知,原因是Android8对隐式广播有限制,解决办法是给intent添加一个flag:intent.addFlags(0x01000000);
如下所示。
private void timeUpdated(boolean is24Hour) {
Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED);
int timeFormatPreference =
is24Hour ? Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR
: Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR;
timeChanged.putExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, timeFormatPreference);
timeChanged.addFlags(0x01000000);
mContext.sendBroadcast(timeChanged);
}
参考链接:
Android8.1 SystemUI源码分析之 电池时钟刷新
android Widget添加过程和android添加widget不更新的问题分析解决
在Android8.0上突破隐式广播的限制