另一種方法,我在日期改變時採取行動。
事實上,Intent.ACTION_DATE_CHANGED只適用於高級日期的日期,而不適用於手動更改電話上的日期時的上一個日期。此外,如果您將時間設置爲11:59並等待它變爲00:00,以便爲您提供日期更改的廣播事件,則不會給您觸發器。因此,我最終制作了自己的存儲共享pref日期的邏輯,然後檢查當前日期是在先前存儲的日期之後還是之前(有一些日期處理邏輯),並基於此更新我的數據。
private void takesomeActionIfDateIsChanged() {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
long currentDateTimeInMillis = System.currentTimeMillis();
Date currentDateWithMillis = new Date(currentDateTimeInMillis);
String formattedCurrentDateWithOnlyDMYStr = dateFormat.format(currentDateWithMillis);
Log.d(TAG, "formattedCurrentDate With Only DateMonthAndYear String : " + formattedCurrentDateWithOnlyDMYStr);
Date formattedCurrentDate = dateFormat.parse(formattedCurrentDateWithOnlyDMYStr);
String storedDateStr = mSpManager.getStoredDate(SPManager.STORED_DATE);
Log.d(TAG, "storedDateStr : " + storedDateStr);
Date storedDateOnlyWithDateMonthAndYear = dateFormat.parse(storedDateStr);
if (formattedCurrentDate.after(storedDateOnlyWithDateMonthAndYear)) {
Log.d(TAG, "The current date is after stored date");
mSpManager.putNewDate(SPManager.STORED_DATE, formattedCurrentDateWithOnlyDMYStr);
mCounter = 0;
} else if (formattedCurrentDate.before(storedDateOnlyWithDateMonthAndYear)) {
Log.d(TAG, "The current date is before stored date");
mSpManager.putNewDate(SPManager.STORED_DATE, formattedCurrentDateWithOnlyDMYStr);
mCounter = 0;
} else {
Log.d(TAG, "The current date is same as stored date");
}
} catch (Exception e) {
Log.e(TAG, "exception : ", e);
}
}
是有,我需要在我的清單中添加任何權限? – Lukap
@Lukap沒有需要的權限。 –