Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

broadcast Intent when date changes?

Tags:

android

I would like to know whether there is an android intent to check for change in the system date.I want to an intent that is broadcasted when time changes from 23:59 to 00:00 (the next date).I searched the net and found about android.intent.action.DATE_CHANGED but it seems like this intent is broadcast only if the date is changed manually.

If there is no intent like this let me know if an alternative exists.One alternative i can think of is using an AlarmManger and broadcast a pending intent.Is there anything simpler than this?

like image 456
rogerstone Avatar asked Feb 08 '11 19:02

rogerstone


People also ask

How does intent work with broadcast receiver?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.

What is the life cycle of broadcast?

Latest Android Aptitude Question SOLUTION: What is the life cycle of broadcast receivers in android? Options 1) send intent() 2) onRecieve() 3) implicitBroadcast() 4) sendBroadcast(), sendOrderBroadcast(), and sendStickyB.

What is the time limit for completion of broadcast receiver session?

In general, you want onReceive() to return in under a millisecond, in case your UI is in the foreground, so you do not freeze the UI (a.k.a., have "jank"). There is also a 5-10 second limit, after which Android will basically crash your app.

Is broadcast receiver deprecated?

As per the provided link in the teacher's notes, https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#MonitorChanges declaring BroadcastReceivers in the manifest is deprecated from Android 7.0 and up.


2 Answers

Use AlarmManager and schedule an alarm for midnight.

LINK: A small tutorial to AlarmManager.

btw, I was trying to find this answer ! Thanks.

like image 89
Piyush Patel Avatar answered Nov 14 '22 14:11

Piyush Patel


This should not be hard to implement using AlarmManager. You just need the following few statements to trigger your app by setting a pending intent on the alarm manager.

Context.getSystemService(Context.ALARM_SERVICE);

Remove any alarms with a matching Intent. Do this first.

cancel(PendingIntent operation);

Schedule an alarm. Just add 24 hrs or whatever makes sense for your need.

set(int type, long triggerAtTime, PendingIntent operation);
like image 30
dipu Avatar answered Nov 14 '22 15:11

dipu