您好,登錄后才能下訂單哦!
這篇文章主要講解了“Android時間設置問題怎么解決”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Android時間設置問題怎么解決”吧!
最近處理了一個非常有意思的系統bug,修改系統時間,重啟后居然沒有生效
注意要關閉使用網絡提供的時間和使用網絡提供的時區這兩個開關。
重啟后顯示的時間日期為
顯示的時間既不是我設置的時間,也不是當前時間(當前時間為2023-03-20 15:49),那么顯示的這個時間到底是什么時間呢?
為了弄清楚這個問題,我研究了一下Android設置時間的邏輯,研究過程中還發現了一些彩蛋。
首先是設置時間的邏輯,源碼位于packages/apps/Settings/src/com/android/settings/datetime/DatePreferenceController.java
public class DatePreferenceController extends AbstractPreferenceController implements PreferenceControllerMixin, DatePickerDialog.OnDateSetListener { //省略部分代碼 private final DatePreferenceHost mHost; @Override public boolean handlePreferenceTreeClick(Preference preference) { //點擊日期后處理 if (!TextUtils.equals(preference.getKey(), KEY_DATE)) { return false; } //顯示日期選擇框 mHost.showDatePicker(); return true; } //省略部分代碼 }
mHost
是DatePreferenceHost
接口,接口實現在packages/apps/Settings/src/com/android/settings/DateTimeSettings.java
中,因此,showDatePicker()
的邏輯位于該實現類中
@SearchIndexable public class DateTimeSettings extends DashboardFragment implements TimePreferenceController.TimePreferenceHost, DatePreferenceController.DatePreferenceHost { //省略部分代碼 @Override public void showDatePicker() { //顯示日期選擇對話框 showDialog(DatePreferenceController.DIALOG_DATEPICKER); } //省略部分代碼 }
showDialog()
定義在父類packages/apps/Settings/src/com/android/settings/SettingsPreferenceFragment.java
中
public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment implements DialogCreatable, HelpResourceProvider, Indexable { protected void showDialog(int dialogId) { if (mDialogFragment != null) { Log.e(TAG, "Old dialog fragment not null!"); } //創建SettingsDialogFragment并進行show mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId); mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId)); } }
showDialog()
中就是創建了SettingsDialogFragment
然后顯示,SettingsDialogFragment
是SettingsPreferenceFragment
的一個內部類,看一下SettingsDialogFragment
的定義
public static class SettingsDialogFragment extends InstrumentedDialogFragment { private static final String KEY_DIALOG_ID = "key_dialog_id"; private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id"; private Fragment mParentFragment; private DialogInterface.OnCancelListener mOnCancelListener; private DialogInterface.OnDismissListener mOnDismissListener; public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) { if (!(fragment instanceof Fragment)) { throw new IllegalArgumentException("fragment argument must be an instance of " + Fragment.class.getName()); } final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment(); settingsDialogFragment.setParentFragment(fragment); settingsDialogFragment.setDialogId(dialogId); return settingsDialogFragment; } @Override public int getMetricsCategory() { if (mParentFragment == null) { return Instrumentable.METRICS_CATEGORY_UNKNOWN; } final int metricsCategory = ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId); if (metricsCategory <= 0) { throw new IllegalStateException("Dialog must provide a metrics category"); } return metricsCategory; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (mParentFragment != null) { outState.putInt(KEY_DIALOG_ID, mDialogId); outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId()); } } @Override public void onStart() { super.onStart(); if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) { ((SettingsPreferenceFragment) mParentFragment).onDialogShowing(); } } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { if (savedInstanceState != null) { mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0); mParentFragment = getParentFragment(); int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1); if (mParentFragment == null) { mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId); } if (!(mParentFragment instanceof DialogCreatable)) { throw new IllegalArgumentException( (mParentFragment != null ? mParentFragment.getClass().getName() : mParentFragmentId) + " must implement " + DialogCreatable.class.getName()); } // This dialog fragment could be created from non-SettingsPreferenceFragment if (mParentFragment instanceof SettingsPreferenceFragment) { // restore mDialogFragment in mParentFragment ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this; } } //通過DialogCreatable接口剝離了dialog的創建 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId); } @Override public void onCancel(DialogInterface dialog) { super.onCancel(dialog); if (mOnCancelListener != null) { mOnCancelListener.onCancel(dialog); } } @Override public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); if (mOnDismissListener != null) { mOnDismissListener.onDismiss(dialog); } } public int getDialogId() { return mDialogId; } @Override public void onDetach() { super.onDetach(); // This dialog fragment could be created from non-SettingsPreferenceFragment if (mParentFragment instanceof SettingsPreferenceFragment) { // in case the dialog is not explicitly removed by removeDialog() if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) { ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null; } } } private void setParentFragment(DialogCreatable fragment) { mParentFragment = (Fragment) fragment; } private void setDialogId(int dialogId) { mDialogId = dialogId; } }
很標準的自定義DialogFragment
的模板代碼,核心代碼在onCreateDialog()
方法當中,但此方法通過DialogCreatable
接口剝離了dialog的創建,這里也很好理解,因為不僅有設置日期的Dialog,還有設置時間的Dialog,如果寫死的話,那么就需要定義兩個DialogFragment
,所以這里它給抽象出來了,DialogCreatable
接口的實現仍然在DateTimeSettings
當中,它的父類SettingsPreferenceFragment
實現了DialogCreatable
@SearchIndexable public class DateTimeSettings extends DashboardFragment implements TimePreferenceController.TimePreferenceHost, DatePreferenceController.DatePreferenceHost { //省略部分代碼 @Override public Dialog onCreateDialog(int id) { //根據選項創建對應的dialog switch (id) { case DatePreferenceController.DIALOG_DATEPICKER: return use(DatePreferenceController.class) .buildDatePicker(getActivity()); case TimePreferenceController.DIALOG_TIMEPICKER: return use(TimePreferenceController.class) .buildTimePicker(getActivity()); default: throw new IllegalArgumentException(); } } //省略部分代碼 }
根據用戶選擇的操作(設置日期or設置時間),創建對應的dialog,最終的創建過程由DatePreferenceController
來完成
public class DatePreferenceController extends AbstractPreferenceController implements PreferenceControllerMixin, DatePickerDialog.OnDateSetListener { //省略部分代碼 public DatePickerDialog buildDatePicker(Activity activity) { final Calendar calendar = Calendar.getInstance(); //創建DatePickerDialog final DatePickerDialog d = new DatePickerDialog( activity, this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); // The system clock can't represent dates outside this range. calendar.clear(); calendar.set(2007, Calendar.JANUARY, 1); //設置最小時間為2007-01-01 d.getDatePicker().setMinDate(calendar.getTimeInMillis()); calendar.clear(); calendar.set(2037, Calendar.DECEMBER, 31); //設置最大時間為2037-12-31 d.getDatePicker().setMaxDate(calendar.getTimeInMillis()); return d; } //省略部分代碼 }
這里可以看到,系統限制了可選的日期范圍為2007-01-01至2037-12-31,實際操作也確實是這樣子的(開發板和小米手機都是),此為彩蛋1。
看一下DatePickerDialog
的定義
public class DatePickerDialog extends AlertDialog implements OnClickListener, OnDateChangedListener { private static final String YEAR = "year"; private static final String MONTH = "month"; private static final String DAY = "day"; @UnsupportedAppUsage private final DatePicker mDatePicker; private OnDateSetListener mDateSetListener; //省略部分代碼 private DatePickerDialog(@NonNull Context context, @StyleRes int themeResId, @Nullable OnDateSetListener listener, @Nullable Calendar calendar, int year, int monthOfYear, int dayOfMonth) { super(context, resolveDialogTheme(context, themeResId)); final Context themeContext = getContext(); final LayoutInflater inflater = LayoutInflater.from(themeContext); //初始化Dialog的View final View view = inflater.inflate(R.layout.date_picker_dialog, null); setView(view); setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this); setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this); setButtonPanelLayoutHint(LAYOUT_HINT_SIDE); if (calendar != null) { year = calendar.get(Calendar.YEAR); monthOfYear = calendar.get(Calendar.MONTH); dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); } mDatePicker = (DatePicker) view.findViewById(R.id.datePicker); mDatePicker.init(year, monthOfYear, dayOfMonth, this); mDatePicker.setValidationCallback(mValidationCallback); mDateSetListener = listener; } //省略部分代碼 /** * Sets the listener to call when the user sets the date. * * @param listener the listener to call when the user sets the date */ public void setOnDateSetListener(@Nullable OnDateSetListener listener) { mDateSetListener = listener; } @Override public void onClick(@NonNull DialogInterface dialog, int which) { switch (which) { case BUTTON_POSITIVE: if (mDateSetListener != null) { // Clearing focus forces the dialog to commit any pending // changes, e.g. typed text in a NumberPicker. mDatePicker.clearFocus(); //設置完成回調 mDateSetListener.onDateSet(mDatePicker, mDatePicker.getYear(), mDatePicker.getMonth(), mDatePicker.getDayOfMonth()); } break; case BUTTON_NEGATIVE: cancel(); break; } } //省略部分代碼 /** * The listener used to indicate the user has finished selecting a date. */ public interface OnDateSetListener { /** * @param view the picker associated with the dialog * @param year the selected year * @param month the selected month (0-11 for compatibility with * {@link Calendar#MONTH}) * @param dayOfMonth the selected day of the month (1-31, depending on * month) */ void onDateSet(DatePicker view, int year, int month, int dayOfMonth); } }
可以看到也是標準的自定義Dialog,不過它是繼承的AlertDialog
,設置完成后通過OnDateSetListener
進行回調,而DatePreferenceController
實現了該接口
public class DatePreferenceController extends AbstractPreferenceController implements PreferenceControllerMixin, DatePickerDialog.OnDateSetListener { //省略部分代碼 @Override public void onDateSet(DatePicker view, int year, int month, int day) { //設置日期 setDate(year, month, day); //更新UI mHost.updateTimeAndDateDisplay(mContext); } //省略部分代碼 @VisibleForTesting void setDate(int year, int month, int day) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); c.set(Calendar.DAY_OF_MONTH, day); //設置日期與定義的最小日期取最大值,也就意味著設置的日期不能小于定義的最小日期 long when = Math.max(c.getTimeInMillis(), DatePreferenceHost.MIN_DATE); if (when / 1000 < Integer.MAX_VALUE) { //設置系統時間 ((AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE)).setTime(when); } } }
可以看到系統定義了一個最小日期DatePreferenceHost.MIN_DATE
,其值為2007-11-05 0:00
public interface UpdateTimeAndDateCallback { // Minimum time is Nov 5, 2007, 0:00. long MIN_DATE = 1194220800000L; void updateTimeAndDateDisplay(Context context); }
最終顯示日期會在目標日期和最小日期中取最大值,也就是說設定的日期不能小于最小日期,而上文說到,選擇的日期范圍為2007-01-01至2037-12-31,因此,如果你設置的日期在2007-01-01至2007-11-05之間,最終都會顯示2007-11-05,實際測試也是如此(開發板和小米手機都是),此為彩蛋2。
選擇完時間后,最后通過AlarmManagerService
來設置系統內核的時間,此處涉及到跨進程通信,使用的通信方式是AIDL,直接到AlarmManagerService
看看如何設置內核時間的
class AlarmManagerService extends SystemService { //省略部分代碼 /** * Public-facing binder interface */ private final IBinder mService = new IAlarmManager.Stub() { //省略部分代碼 @Override public boolean setTime(long millis) { //先授權 getContext().enforceCallingOrSelfPermission( "android.permission.SET_TIME", "setTime"); //然后設置系統內核時間 return setTimeImpl(millis); } //省略部分代碼 } //省略部分代碼 boolean setTimeImpl(long millis) { if (!mInjector.isAlarmDriverPresent()) { Slog.w(TAG, "Not setting time since no alarm driver is available."); return false; } synchronized (mLock) { final long currentTimeMillis = mInjector.getCurrentTimeMillis(); //設置系統內核時間 mInjector.setKernelTime(millis); final TimeZone timeZone = TimeZone.getDefault(); final int currentTzOffset = timeZone.getOffset(currentTimeMillis); final int newTzOffset = timeZone.getOffset(millis); if (currentTzOffset != newTzOffset) { Slog.i(TAG, "Timezone offset has changed, updating kernel timezone"); //設置系統內核時區 mInjector.setKernelTimezone(-(newTzOffset / 60000)); } // The native implementation of setKernelTime can return -1 even when the kernel // time was set correctly, so assume setting kernel time was successful and always // return true. return true; } } //省略部分代碼 @VisibleForTesting static class Injector { //省略部分代碼 void setKernelTime(long millis) { Log.d("jasonwan", "setKernelTime: "+millis); if (mNativeData != 0) { //在native層完成內核時間的設置 AlarmManagerService.setKernelTime(mNativeData, millis); } } //省略部分代碼 } //native層完成 private static native int setKernelTime(long nativeData, long millis); private static native int setKernelTimezone(long nativeData, int minuteswest); //省略部分代碼 }
可以看到最終是在native層完成內核時間的設置,這也理所當然,畢竟java是應用層,觸及不到kernel層。
回到最開始的問題,為啥開機之后卻不是我們設置的時間呢,這就要看看開機之后系統是怎么設置時間的。同樣在AlarmManagerService
里面,因為它是SystemService
的子類,所以會隨著開機啟動而啟動,而Service啟動后必定會執行它的生命周期方法,設置時間的邏輯就是在onStart()
生命周期方法里面
class AlarmManagerService extends SystemService { //省略部分代碼 @Override public void onStart() { mInjector.init(); synchronized (mLock) { //省略部分代碼 // We have to set current TimeZone info to kernel // because kernel doesn't keep this after reboot //設置時區,從SystemProperty中讀取 setTimeZoneImpl(SystemProperties.get(TIMEZONE_PROPERTY)); // Ensure that we're booting with a halfway sensible current time. Use the // most recent of Build.TIME, the root file system's timestamp, and the // value of the ro.build.date.utc system property (which is in seconds). //設置時區 //先讀取系統編譯時間 long utc = 1000L * SystemProperties.getLong("ro.build.date.utc", -1L); //再讀取根目錄最近的修改的時間 long lastModified = Environment.getRootDirectory().lastModified(); //然后讀取系統構建時間,三個時間取最大值 final long systemBuildTime = Long.max( utc, Long.max(lastModified, Build.TIME)); //代碼1 Log.d("jasonwan", "onStart: utc="+utc+", lastModified="+lastModified+", BuildTime="+Build.TIME+", currentTimeMillis="+mInjector.getCurrentTimeMillis()); //設置的時間小于最大值,則將最大值設置為系統內核的時間,注意,因為我們剛剛已經設置了內核時間,所以重啟后通過System.currentTimeMillis()得到的時間戳為我們設置的時間,此判斷意味著,系統編譯時間、根目錄最近修改時間、系統構建時間、設置的時間,這四者當中取最大值作為重啟后的內核時間 if (mInjector.getCurrentTimeMillis() < systemBuildTime) { //這里mInjector.getCurrentTimeMillis()其實就是System.currentTimeMillis() Slog.i(TAG, "Current time only " + mInjector.getCurrentTimeMillis() + ", advancing to build time " + systemBuildTime); mInjector.setKernelTime(systemBuildTime); } //省略部分代碼 } //省略部分代碼 @VisibleForTesting static class Injector { //省略部分代碼 void setKernelTimezone(int minutesWest) { AlarmManagerService.setKernelTimezone(mNativeData, minutesWest); } void setKernelTime(long millis) { //代碼2 Log.d("jasonwan", "setKernelTime: "+millis); if (mNativeData != 0) { AlarmManagerService.setKernelTime(mNativeData, millis); } } //省略部分代碼 long getElapsedRealtime() { return SystemClock.elapsedRealtime(); } long getCurrentTimeMillis() { return System.currentTimeMillis(); } //省略部分代碼 } }
根據源碼分析得知,系統最終會在系統編譯時間、根目錄最近修改時間、系統構建時間、設置的時間,這四者當中取最大值作為重啟后的內核時間,這里我在代碼1和代碼2處埋下了log,看看四個時間的值分別是多少,以及最終設置的內核時間是多少,我在設置中手動設置的日期為2022-10-01,重啟后的日志如下
四個值分別為:
系統編譯時間:1669271830000,格式化后為2022-11-24 14:37:10
根目錄最近修改時間:1678865533000,格式化后為2023-03-15 15:32:13
構建時間:1669271830000,同系統編譯時間
設置的時間:1664609754998,格式化后為2022-10-01 15:35:54
注意,我們只需要注意日期,不需要關注時分秒,可以看到四個時間當中,最大的為根目錄最近修改時間,所以最終顯示的日期為2023-03-15,此為彩蛋3。
我在開發板和小米手機上測試的結果相同,說明MIUI保留了這一塊的邏輯,但是MIUI也有一個bug,就是明明我關閉了使用網絡提供的時間和使用網絡提供的時區,它還是給我自動更新了日期和時間,除非開啟飛行模式之后才不自動更新。
同時我們還注意到,系統編譯時間ro.build.date.utc
跟系統構建時間Build.TIME
是相同的,這很好理解,編譯跟構建是一個意思,而且Build.TIME
的取值其實也來自于ro.build.date.utc
/** * Information about the current build, extracted from system properties. */ public class Build { //省略部分代碼 /** The time at which the build was produced, given in milliseconds since the UNIX epoch. */ public static final long TIME = getLong("ro.build.date.utc") * 1000; //省略部分代碼 }
感謝各位的閱讀,以上就是“Android時間設置問題怎么解決”的內容了,經過本文的學習后,相信大家對Android時間設置問題怎么解決這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。