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.
Asked
Active
Viewed 1.1k times
4
-
2possible duplicate of [Date and time change listener in Android?](http://stackoverflow.com/questions/5481386/date-and-time-change-listener-in-android) – Muhammed Refaat Dec 07 '14 at 13:06
-
1possible duplicate of http://stackoverflow.com/questions/14888773/broadcast-receiver-at-a-specific-time – Atish Agrawal Dec 07 '14 at 13:08
2 Answers
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.

gaurav jain
- 3,119
- 3
- 31
- 48
-
1its not working in android x. can you share the working example code? – Shahid Ghafoor Sep 28 '20 at 09:58
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)
}

extmkv
- 1,991
- 1
- 18
- 36
-
1The 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) – Jayman Jani May 12 '20 at 09:12