2011-10-27 166 views
2

所以我正在試圖獲得一些意圖爲我工作, 和我有2個具體意圖的問題。我搜索了這些組 和網站,似乎找不到比文檔給我提供的任何更多信息 。 它似乎沒有,如果我宣佈在 清單中使用這些意圖到物質,或者如果我嘗試註冊代碼的接收器,接收器我從未 收到ACTION_TIME_CHANGEDACTION_DATE_CHANGED的行動。 至於我可以告訴我應該 來接收我 廣播接收這些動作,每當用戶明確地設置時間/日期要"Settings" --> "Date & Time",然後修改時間或 日期 上午我錯解釋什麼,這些行動對於? 是否有任何權限需要聲明才能接收 這些操作? 有沒有人使用這個動作?ACTION_TIME_CHANGED或ACTION_DATE_CHANGED,無法讓他們工作

是否有一個地方這個動作時,我無法找到任何有用的一些有用的例子,有很多的格式變化的例子,但不是實際的變化TIME_DATE ...

在此先感謝

回答

0

在您的廣播接收器onReceive方法中,我假設你有類似於以下的設置?

@Override 
public void onReceive(Context context, Intent intent){ 
    final String action = intent.getAction(); 
    if (Intent.ACTION_DATE_CHANGED.equals(action) || Intent.ACTION_TIME_CHANGED.equals(action)){ 
    // do stuff here 
    } 
    ... 
} 

另一個要考慮的是你正確的過濾器添加到您的IntentFilter如...

IntentFilter filter = new IntentFilter(); 
filter.addAction(Intent.ACTION_TIME_CHANGED); 
filter.addAction(Intent.ACTION_DATE_CHANGED); 
+0

是有,我需要在我的清單中添加任何權限? – Lukap

+0

@Lukap沒有需要的權限。 –

1

是的,你必須添加

<intent-filter> 
    <action android:name="android.intent.action.DATE_CHANGED"></action> 
</intent-filter> 
2

Intent.ACTION_DATE_CHANGEDIntent.ACTION_TIME_CHANGE d被觸發只有當用戶手動更改時間或日期時,它們纔不會被系統自動觸發。

我擔心你不得不使用Intent.ACTION_TIME_TICK,這是系統每分鐘精確觸發的。

5

除了<intent-filter>,看到Android ACTION_DATE_CHANGED broadcast關於AOSP的錯誤:當時鐘被設置ACTION_TIME_CHANGED應該火,當時鍾滴答到第二天ACTION_DATE_CHANGED應該火,但如果時鐘被設置反了,ACTION_DATE_CHANGED韓元」再次開火直到時鐘趕上第二天的事情。

+0

你知道如何用ACTION_DATE_CHANGED解決這個問題嗎? –

+0

@hemanthkumar請問https://stackoverflow.com/q/21758246/1682419有幫助嗎?另請注意Android O上的[通過清單註冊隱式廣播意圖]中的更改(https://developer.android.com/about/versions/oreo/background.html#broadcasts) – Jerry101

0

另一種方法,我在日期改變時採取行動。

事實上,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); 
    } 
} 
+0

「Date」類相當廢棄,並且不需要格式化日期以檢查值,因此您可以在此上保存一些CPU。你應該考慮使用'Calendar'或者甚至更好:'LocalDate'(通過一個庫可以訪問舊的Android版本:https://github.com/JakeWharton/ThreeTenABP)。 'LocalDate'在內存和CPU中效率更高。通過這種方式,可以非常容易地檢查當前日期與前一日期不同,並且如果使用Kotlin,則甚至可以使用正常的比較符號(例如'!=')。 –

+0

@Richa,_當你將時間設定爲11:59並等待它變成00:00,以便給你日期改變的廣播事件時,它不會給你觸發器._ - >這對我有效。 – Joshua

0

您不需要任何權限。

爲了獲得日期更改的通知,您需要註冊到BroadcastReceiver(以及ACTION_TIMEZONE_CHANGED)。

這裏是我做了一個解決方案:

https://stackoverflow.com/a/48782963/878126