主要是参考了息屏时间设置的Preference。
增加DateFormatController.java
和DateFormatListPreference.java
这2个文件。
//vendor\mediatek\proprietary\packages\apps\MtkSettings\src\com\android\settings\datetime\DateFormatController.java
public class DateFormatListPreference extends RestrictedListPreference{
private final CharSequence[] mInitialEntries;
private final CharSequence[] mInitialValues;
public DateFormatListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mInitialEntries = getEntries();
mInitialValues = getEntryValues();
}
}
intent发送前要调用addFlags(0x01000000),否则会报错Background execution not allowed:******************
//vendor\mediatek\proprietary\packages\apps\MtkSettings\src\com\android\settings\datetime\DateFormatController.java
public class DateFormatController extends AbstractPreferenceController implements
PreferenceControllerMixin, Preference.OnPreferenceChangeListener{
private static final String TAG = "DateFormatController";
private final String mDateFormatKey;
public DateFormatController(Context context, String key) {
super(context);
mDateFormatKey = key;
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return mDateFormatKey;
}
@Override
public void updateState(Preference preference) {
final DateFormatListPreference dateFormatListPreference = (DateFormatListPreference) preference;
final String value = Settings.System.getString(mContext.getContentResolver(), DATE_FORMAT);
dateFormatListPreference.setValue(value);
preference.setSummary(getDate(value));
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String value = (String) newValue;
Settings.System.putString(mContext.getContentResolver(), DATE_FORMAT, value);
preference.setSummary(getDate(value));
sendDateFormatUpdateBroadcast();
return true;
}
private String getDate(String dateFormat){
Date date = new Date(System.currentTimeMillis());
String dateString = new SimpleDateFormat(dateFormat).format(date);
return dateString;
}
private void sendDateFormatUpdateBroadcast() {
Intent dateFormatUpdate = new Intent("android.intent.action.DATE_FORMAT_UPDATE");
dateFormatUpdate.addFlags(0x01000000);
mContext.sendBroadcast(dateFormatUpdate);
}
}
DateTimeSettings
中增加控制部分。private static final String KEY_DATE_FORMAT = "date_format";
和controllers.add(new DateFormatController(context, KEY_DATE_FORMAT));
具体如下。
public class DateTimeSettings extends DashboardFragment implements
TimePreferenceController.TimePreferenceHost, DatePreferenceController.DatePreferenceHost,
AutoTimeExtPreferenceController.GPSPreferenceHost, DialogInterface.OnCancelListener {
private static final String KEY_DATE_FORMAT = "date_format";
................
@Override
protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
................
controllers.add(new DateFormatController(context, KEY_DATE_FORMAT));
................
}
}
date_time_prefs中增加DateFormatListPreference。
//vendor\mediatek\proprietary\packages\apps\MtkSettings\res\xml\date_time_prefs.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
android:title="@string/date_and_time"
settings:keywords="@string/keywords_date_and_time">
............
<com.android.settings.DateFormatListPreference
android:key="date_format"
android:title="@string/title_date_format"
android:summary="@string/summary_placeholder"
android:entries="@array/date_format_entries"
android:entryValues="@array/date_format_values" />
<SwitchPreference
android:key="24 hour"
android:title="@string/date_time_24hour" />
</PreferenceScreen>
定义相关string和array 。
//vendor\mediatek\proprietary\packages\apps\MtkSettings\res\values\arrays.xml
<string-array name="date_format_entries">
<item>year-month-day</item>
<item>month-day-year</item>
<item>day-month-year</item>
</string-array>
<string-array name="date_format_values" translatable="false">
<!-- Do not translate. -->
<item>yyyy-MM-dd</item>
<!-- Do not translate. -->
<item>MM-dd-yyyy</item>
<!-- Do not translate. -->
<item>dd-MM-yyyy</item>
</string-array>
//vendor\mediatek\proprietary\packages\apps\MtkSettings\res\values\strings.xml
<string name="title_date_format">date format</string>
//vendor\mediatek\proprietary\packages\apps\MtkSettings\res\values-zh-rCN\arrays.xml
<string-array name="date_format_entries">
<item>年-月-日</item>
<item>月-日-年</item>
<item>日-月-年</item>
</string-array>
//vendor\mediatek\proprietary\packages\apps\MtkSettings\res\values-zh-rCN\strings.xml
<string name="title_date_format">日期格式</string>
SettingsProvider中增加Settings.System.DATE_FORMAT默认设置。
//vendor\mediatek\proprietary\packages\apps\SettingsProvider\src\com\android\providers\settings\DatabaseHelper.java
private void loadSystemSettings(SQLiteDatabase db) {
SQLiteStatement stmt = null;
try {
stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
+ " VALUES(?,?);");
loadStringSetting(stmt,Settings.System.DATE_FORMAT,R.string.def_date_format);
....................
}
//vendor\mediatek\proprietary\packages\apps\SettingsProvider\res\values\defaults.xml
<string name="def_date_format" translatable="false">yyyy-MM-dd</string>
以下为桌面时钟布局修改部分。
AndroidManifest.xml中增加protected-broadcast定义,否则发送广播后会报错Sending non-protected broadcast
,无法接受到广播。
增加<protected-broadcast android:name="android.intent.action.DATE_FORMAT_UPDATE" />
//frameworks/base/core/res/AndroidManifest.xml
<protected-broadcast android:name="android.intent.action.SCREEN_OFF" />
<protected-broadcast android:name="android.intent.action.SCREEN_ON" />
<protected-broadcast android:name="android.intent.action.USER_PRESENT" />
<protected-broadcast android:name="android.intent.action.TIME_SET" />
<protected-broadcast android:name="android.intent.action.TIME_TICK" />
<protected-broadcast android:name="android.intent.action.TIMEZONE_CHANGED" />
<protected-broadcast android:name="android.intent.action.DATE_CHANGED" />
<protected-broadcast android:name="android.intent.action.DATE_FORMAT_UPDATE" />
<protected-broadcast android:name="android.intent.action.PRE_BOOT_COMPLETED" />
<protected-broadcast android:name="android.intent.action.BOOT_COMPLETED" />
DigitalAppWidgetProvider
中有3处修改。
-
增加
private static final String ACTION_DATE_FORMAT_UPDATE = "android.intent.action.DATE_FORMAT_UPDATE";
-
增加当收到广播时更新UI。
case ACTION_DATE_FORMAT_UPDATE:
Log.d(TAG,"ACTION_DATE_FORMAT_UPDATE");
for (int widgetId : widgetIds) {
relayoutWidget(context, wm, widgetId, wm.getAppWidgetOptions(widgetId));
}
break;
3.修改getDateFormat(Context context),修改中文和英文语言模式下的日期格式 。
增加
if (locale.getCountry().equals("CN")){
return Settings.System.getString(context.getContentResolver(), DATE_FORMAT) + " EEE";
}else if (locale.getCountry().equals("US")){
return "EEE, " + Settings.System.getString(context.getContentResolver(), DATE_FORMAT);
}
修改后如下。
//vendor/mediatek/proprietary/packages/apps/DeskClock/src/com/android/alarmclock/DigitalAppWidgetProvider.java
public class DigitalAppWidgetProvider extends AppWidgetProvider {
private static final String ACTION_DATE_FORMAT_UPDATE = "android.intent.action.DATE_FORMAT_UPDATE";
@Override
public void onReceive(@NonNull Context context, @NonNull Intent intent) {
...........
final String action = intent.getAction();
switch (action) {
case ACTION_NEXT_ALARM_CLOCK_CHANGED:
case ACTION_DATE_CHANGED:
case ACTION_LOCALE_CHANGED:
case ACTION_SCREEN_ON:
case ACTION_TIME_CHANGED:
case ACTION_TIMEZONE_CHANGED:
case ACTION_ALARM_CHANGED:
case ACTION_ON_DAY_CHANGE:
case ACTION_WORLD_CITIES_CHANGED:
for (int widgetId : widgetIds) {
relayoutWidget(context, wm, widgetId, wm.getAppWidgetOptions(widgetId));
}
case ACTION_DATE_FORMAT_UPDATE:
Log.d(TAG,"ACTION_DATE_FORMAT_UPDATE");
for (int widgetId : widgetIds) {
relayoutWidget(context, wm, widgetId, wm.getAppWidgetOptions(widgetId));
}
break;
}
...........
}
/**
* @return the locale-specific date pattern
*/
private static String getDateFormat(Context context) {
final Locale locale = Locale.getDefault();
if (locale.getCountry().equals("CN")){
return Settings.System.getString(context.getContentResolver(), DATE_FORMAT) + " EEE";
}else if (locale.getCountry().equals("US")){
return "EEE, " + Settings.System.getString(context.getContentResolver(), DATE_FORMAT);
}
final String skeleton = context.getString(R.string.abbrev_wday_month_day_no_year);
return DateFormat.getBestDateTimePattern(locale, skeleton);
}
}
receiver增加<action android:name="android.intent.action.DATE_FORMAT_UPDATE" />
定义。
//vendor/mediatek/proprietary/packages/apps/DeskClock/AndroidManifest.xml
<receiver
android:name="com.android.alarmclock.DigitalAppWidgetProvider"
android:label="@string/digital_gadget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.app.action.NEXT_ALARM_CLOCK_CHANGED" />
<action android:name="android.intent.action.DATE_CHANGED" />
<action android:name="android.intent.action.LOCALE_CHANGED" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.DATE_FORMAT_UPDATE" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="com.android.deskclock.ALARM_CHANGED" />
<action android:name="com.android.deskclock.ON_DAY_CHANGE" />
<action android:name="com.android.deskclock.WORLD_CITIES_CHANGED" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/digital_appwidget" />
</receiver>
参考链接:
在Android8.0上突破隐式广播的限制
[Q]Sending non-protected broadcast问题分析
日期操作类(DateFormat与SimpleDateFormat)的区别和使用详解
网友评论