3

So I am in the process of trying to get a few intents to work for me, and am having problems on 2 specific intents. I searched these groups and the webs and can 't seem to find any more information than what the documentation gives me. It doesn't seem to matter if I declare the use of these intent in the manifest, or if I try to register a receiver in code, my receiver never receives the actions for ACTION_TIME_CHANGED or ACTION_DATE_CHANGED. As far as I can tell I should be receiving these actions in my broadcast receiver whenever the user explicitly sets the time/date by going to "Settings" --> "Date & Time" and then modifying the time or date Am I mis-interpreting what these actions are for? Are there any permissions that I need to declare in order to receive these actions? Does anybody worked with this actions ?

Is there some useful example where this actions are used, I could not find anything useful, there is plenty of examples of format change but not on actual time_date change...

Thanks in advance

Shobi
  • 10,374
  • 6
  • 46
  • 82
Lukap
  • 31,523
  • 64
  • 157
  • 244

6 Answers6

5

In addition to the <intent-filter>, see Android ACTION_DATE_CHANGED broadcast about an AOSP bug: ACTION_TIME_CHANGED should fire when the clock gets set and ACTION_DATE_CHANGED should fire when the clock ticks over to the next day, but if the clock gets set backwards, ACTION_DATE_CHANGED won't fire again until the clock catches up with what was going to be the next day.

Community
  • 1
  • 1
Jerry101
  • 12,157
  • 5
  • 44
  • 63
  • Do you know how to overcome the issue with ACTION_DATE_CHANGED? – hemanth kumar Jul 06 '17 at 05:51
  • 1
    @hemanthkumar Does https://stackoverflow.com/q/21758246/1682419 help? Also note changes in Android O on [registering for implicit broadcast intents via the manifest](https://developer.android.com/about/versions/oreo/background.html#broadcasts) – Jerry101 Sep 06 '17 at 01:33
3

Intent.ACTION_DATE_CHANGED and Intent.ACTION_TIME_CHANGED are triggered only when the user manually changes time or date, and they are not automatically triggered by the system.

I'm afraid you have to use Intent.ACTION_TIME_TICK, which is triggered by the system exactly each minute.

Jorn Rigter
  • 745
  • 1
  • 6
  • 25
Santacrab
  • 3,165
  • 2
  • 25
  • 31
1

You don't need any permission.

In order to be notified of date changes, you need to register to a BroadcastReceiver with those actions (and also ACTION_TIMEZONE_CHANGED) .

Here's a solution I've made:

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

android developer
  • 114,585
  • 152
  • 739
  • 1,270
1

Yes, you must add

<intent-filter>
   <action android:name="android.intent.action.DATE_CHANGED"></action>
</intent-filter>
oers
  • 18,436
  • 13
  • 66
  • 75
Shahzad Imam
  • 2,030
  • 2
  • 26
  • 45
0

An alternative approach which I used to take an action when the date is changed.

Actually, the Intent.ACTION_DATE_CHANGED only works with the date of an advanced date and not with a previous date when you change the date on the phone manually. Also, when you set time to 11:59 and wait for it to become 00:00 to give you the broadcast event of the date change, it doesn't give you the trigger.

So, I ended up making my own logic of storing a date in shared pref and then checking if the current date is after or before previously stored date (with some date processing logic) and updating my data based on that.

  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);
    }
}
Richa
  • 700
  • 8
  • 12
  • 1
    The class `Date` is quite deprecated, and formatting the date is not needed in order to check values, so you can save some CPU on this. You should consider using `Calendar` or even better: `LocalDate` (avialable for old Android versions via a library : https://github.com/JakeWharton/ThreeTenABP) . The `LocalDate` is much more efficient, in both memory and CPU. This way, it can be very easy to check that the current date is different than the previous one, and if you use Kotlin, you can even use normal comparison signs (for example `!=` ) . – android developer Jan 21 '18 at 14:51
  • @Richa, _when you set time to 11:59 and wait for it to become 00:00 to give you the broadcast event of the date change, it doesn't give you the trigger._ -> This worked for me. – Joshua Feb 05 '18 at 05:56
  • @Joshua Sometimes it works sometimes it doesn't. It is confirmed bug in AOSP and it is not reliable. – Torima Apr 15 '22 at 12:13
0

Inside your broadcast receiver onReceive method I am assuming u have a similar setup to the following?

@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
    }
    ...
}

Another consideration would be did you add the proper filters to your IntentFilter such as...

IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_DATE_CHANGED);
Maurycy
  • 3,911
  • 8
  • 36
  • 44