Android change listener date and time?

My application has an alarm service, and I find that if the user changes the date or time to the elapsed time. My alarm will not be triggered while I expect.

So, I may need to reset all alarms again. Is there a date and time change listener in android?

+48
android
Mar 30 '11 at 2:59 a.m.
source share
2 answers

Create an intent filter:

static { s_intentFilter = new IntentFilter(); s_intentFilter.addAction(Intent.ACTION_TIME_TICK); s_intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED); s_intentFilter.addAction(Intent.ACTION_TIME_CHANGED); } 

and broadcast receiver:

  private final BroadcastReceiver m_timeChangedReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action.equals(Intent.ACTION_TIME_CHANGED) || action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { doWorkSon(); } } }; 

register receiver:

  public void onCreate() { super.onCreate(); registerReceiver(m_timeChangedReceiver, s_intentFilter); } 

EDIT:

and unregister:

  public void onDestroy() { super.onDestroy(); unregisterReceiver(m_timeChangedReceiver); } 
+69
Jun 03 2018-11-18T00:
source share

In addition to the accepted answer

If you want to listen to time changes while your application is running, I would register in the manifest:

 <receiver android:name="com.your.pacakge.TimeChangeBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.TIME_SET"/> <action android:name="android.intent.action.TIMEZONE_CHANGED"/> </intent-filter> </receiver> 

If you do, do not explicitly specify the registrar in the code using registerReceiver and unregisterReceiver .

Again, this is just a complement to the accepted answer.

+9
Jun 08 '17 at 0:02
source share



All Articles