Detect day change event in broadcast receiver in android
Asked Answered
G

2

5

I want to implement a feature in android app which execute a particular code when ever there is a date change(at 00:00AM) even when my app is not running.

Grantee answered 7/12, 2014 at 13:3 Comment(2)
possible duplicate of Date and time change listener in Android?Celadon
possible duplicate of #14889273Hairline
C
21

I might be late answering this question, but I personally faced the same problem. For day change, you can simply use a broadcast receiver with action "android.intent.action.DATE_CHANGED", and it will trigger whenever the date is changed (either implicitly or explicitly by user). I hope this will help someone who gets to here through Google.

Coincide answered 25/6, 2015 at 10:7 Comment(2)
its not working in android x. can you share the working example code?Khan
As of 2023 - using androidx, compileSdk 33, minSdk 26, targetSdk 33: I got it working with Intent.ACTION_TIME_CHANGED. And every time, I changed the date from the setting, it printed the log in onReceive(). Hope this helps someone.Odericus
C
3

To complete @gaurav-jain answer I've here an example, in this case, to detect if the day has changed:

abstract class DayChangedBroadcastReceiver : BroadcastReceiver() {

    private var date = Date()
    private val dateFormat by lazy { SimpleDateFormat("yyMMdd", Locale.getDefault()) }

    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action

        val currentDate = Date()

        if ((action == Intent.ACTION_TIME_CHANGED || action == Intent.ACTION_TIMEZONE_CHANGED) && !isSameDay(currentDate)) {
            date = currentDate
            onDayChanged()
        }
    }

    private fun isSameDay(currentDate: Date) = dateFormat.format(currentDate) == dateFormat.format(date)

    abstract fun onDayChanged()

    companion object {

        /**
         * Create the [IntentFilter] for the [DayChangedBroadcastReceiver].
         *
         * @return The [IntentFilter]
         */
        fun getIntentFilter() = IntentFilter().apply {
            addAction(Intent.ACTION_TIME_TICK)
            addAction(Intent.ACTION_TIMEZONE_CHANGED)
            addAction(Intent.ACTION_TIME_CHANGED)
        }
    }
}

Create the DayChangedBroadcastReceiver in your activity:

private val dayChangedBroadcastReceiver = object : DayChangedBroadcastReceiver() {

        override fun onDayChanged() {
            // TODO Reload data
        }
    }

Register in your activity/fragment:

 override fun onResume() {
        super.onResume()
        activity?.registerReceiver(dayChangedBroadcastReceiver, DayChangedBroadcastReceiver.getIntentFilter())
    }

Unregister in your activity/fragment:

    override fun onPause() {
        super.onPause()
        activity?.unregisterReceiver(dayChangedBroadcastReceiver)
    }
Canker answered 15/7, 2019 at 9:24 Comment(1)
The logic and code is good but i think it is not going to working for all you should add Intent.ACTION_DATE_CHANGED in filter and checked too like this fun getIntentFilter() = IntentFilter().apply { addAction(Intent.ACTION_TIME_TICK) addAction(Intent.ACTION_DATE_CHANGED) addAction(Intent.ACTION_TIMEZONE_CHANGED) addAction(Intent.ACTION_TIME_CHANGED) } and check condition like if ((action == Intent.ACTION_TIME_CHANGED || action==Intent.ACTION_TIMEZONE_CHANGED || action == Intent.ACTION_DATE_CHANGED)&& !isSameDay(currentDate)Germ

© 2022 - 2024 — McMap. All rights reserved.