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

6 Answers 6

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.

2
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.

1

Yes, you must add

<intent-filter>
   <action android:name="android.intent.action.DATE_CHANGED"></action>
</intent-filter>
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

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);
1
  • @Lukap There are no permissions needed. Commented Feb 14, 2018 at 8:14
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);
    }
}
3
  • 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 : 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 != ) . Commented Jan 21, 2018 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
    Commented Feb 5, 2018 at 5:56
  • @Joshua Sometimes it works sometimes it doesn't. It is confirmed bug in AOSP and it is not reliable.
    – Torima
    Commented Apr 15, 2022 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.