Android系统设置默认24小时制
最近基于Android11,进行了设置系统默认24小时制。 Android 在这块的改动是很小的,各个版本都是差不多的。
下面的修改过程:
一、xml定义默认属性值
public:frameworks/base/packages/SettingsProvider/res/values/default.xml
<!-- Default for setting for Settings.SETTING.TIME_12_24 -->
<string name="def_time_12_24">24</string>
24表示格式为24小时制,系统默认为12小时制
二、java定义属性名称
frameworks/base/core/java/android/provider/Settings.java
我的Android11的系统源码已经有定义了,就不用重新写了, 但是给大家展示一下,也有可能有的版本源码没有定义。
/**
* Display times as 12 or 24 hours
* 12
* 24
*/
public static final String TIME_12_24 = "time_12_24";
@UnsupportedAppUsage
public static final Set<String> PUBLIC_SETTINGS = new ArraySet<>();
static {
PUBLIC_SETTINGS.add(END_BUTTON_BEHAVIOR);
PUBLIC_SETTINGS.add(WIFI_USE_STATIC_IP);
PUBLIC_SETTINGS.add(WIFI_STATIC_IP);
//添加自己定义的属性
PUBLIC_SETTINGS.add(TIME_12_24);
。。。
}
/**
* These entries are considered common between the personal and the managed profile,
* since the managed profile doesn't get to change them.
*/
@UnsupportedAppUsage
private static final Set<String> CLONE_TO_MANAGED_PROFILE = new ArraySet<>();
static {
CLONE_TO_MANAGED_PROFILE.add(DATE_FORMAT);
CLONE_TO_MANAGED_PROFILE.add(HAPTIC_FEEDBACK_ENABLED);
CLONE_TO_MANAGED_PROFILE.add(SOUND_EFFECTS_ENABLED);
CLONE_TO_MANAGED_PROFILE.add(TEXT_SHOW_PASSWORD);
//添加自己定义的属性
CLONE_TO_MANAGED_PROFILE.add(TIME_12_24);
}
/**
* System settings which can be accessed by instant apps.
* @hide
*/
public static final Set<String> INSTANT_APP_SETTINGS = new ArraySet<>();
static {
INSTANT_APP_SETTINGS.add(TEXT_AUTO_REPLACE);
INSTANT_APP_SETTINGS.add(TEXT_AUTO_CAPS);
INSTANT_APP_SETTINGS.add(TEXT_AUTO_PUNCTUATE);
INSTANT_APP_SETTINGS.add(TEXT_SHOW_PASSWORD);
INSTANT_APP_SETTINGS.add(DATE_FORMAT);
INSTANT_APP_SETTINGS.add(FONT_SCALE);
INSTANT_APP_SETTINGS.add(HAPTIC_FEEDBACK_ENABLED);
//添加自己定义的属性
INSTANT_APP_SETTINGS.add(TIME_12_24);
}
上面的代码,在不同的源码代码中是有一定的变化的,但是主要代码基本不变。
三、添加默认属性配置
frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
//找到加载默认System属性的方法
private void loadSystemSettings(SQLiteDatabase db) {
SQLiteStatement stmt = null;
try {
stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
+ " VALUES(?,?);");
loadBooleanSetting(stmt, Settings.System.DIM_SCREEN,
R.bool.def_dim_screen);
loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
R.integer.def_screen_off_timeout);
...
//添加默认属性设置
loadStringSetting(stmt, Settings.System.TIME_12_24, R.string.def_time_12_24);
}
四、设置默认为24小时制
frameworks/base/core/java/android/text/format/DateFormat.java
找到is24HourFormat方法,修改为如下内容:
public static boolean is24HourFormat(Context context, int userHandle) {
final String value = Settings.System.getStringForUser(context.getContentResolver(),
Settings.System.TIME_12_24, userHandle);
//change default 24 , liwenzhi
if (value == null) {
Settings.System.putString(context.getContentResolver(), Settings.System.TIME_12_24,"24");
return true;
}
return value.equals("24");
}
外部暴露的方法中,判断是否是24小时制
Settings的(System/Global/Secure)其他属性也可以参考上面的代码修改默认值。
五、后期应用中修改24小时制
其实很简单,就是修改Settings.System.TIME_12_24的属性即可
//读取
Settings.System.getString(Context.getContentResolver(), Settings.System.TIME_12_24, "12");
//设置
Settings.System.putString(Context.getContentResolver(),Settings.System.TIME_12_24, "24");
从上面源码可以看到Settings.System.TIME_12_24的字符串就是time_12_24
Android其他属性的学习可以看下这篇文章: https://blog.csdn.net/wenzhi20102321/article/details/120029551
六、adb查询和设置24小时制
//查询:
adb shell settings get system time_12_24
//设置24小时制:
adb shell settings set system time_12_24 24
|