基于Android8.1系统测试
需要是系统应用
需要在Manifest.xml中声明如下权限:
1 2 |
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> |
重新定义一下系统常量(在系统里是hide的)
1 2 3 4 |
public static final String EXTRA_TIME_PREF_24_HOUR_FORMAT = "android.intent.extra.TIME_PREF_24_HOUR_FORMAT"; public static final int EXTRA_TIME_PREF_VALUE_USE_12_HOUR = 0; public static final int EXTRA_TIME_PREF_VALUE_USE_24_HOUR = 1; |
读取当前设置值:
1 |
String hour24 = Settings.System.getString(getContext().getContentResolver(), Settings.System.TIME_12_24); |
设置新的值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
mLayout24h.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mSwitch24h.setChecked(!mSwitch24h.isChecked()); String newValue = mSwitch24h.isChecked() ? "24" : "12"; Log.d(LOG_TAG, "newValue: " + newValue); Settings.System.putString(getContext().getContentResolver(), Settings.System.TIME_12_24, newValue); Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED); int timeFormatPreference = mSwitch24h.isChecked() ? EXTRA_TIME_PREF_VALUE_USE_24_HOUR : EXTRA_TIME_PREF_VALUE_USE_12_HOUR; timeChanged.putExtra(EXTRA_TIME_PREF_24_HOUR_FORMAT, timeFormatPreference); getContext().sendBroadcast(timeChanged); } }); |