Android中的date和时间更改监听器?
在我的应用程序中,有一个警报服务,我发现如果用户更改它的date或时间到一段时间。 我的预警不会在我预期的时间被触发。
所以,我可能不得不重新设置所有的警报。 在android中有一个date和时间更改侦听器?
- 即使使用正确的api密钥,Google地图API请求也会被拒绝进行Android自动填充
- 检查Android应用程序是否在后台运行
- 可以在Google Play开发者控制台上更改帐户所有者吗?
- 如何在我的Android客户端上启用堆更新
- Android方法“评价我的应用程序”
创build一个意图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); }
和一个广播接收器:
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(); } } };
注册接收器:
public void onCreate() { super.onCreate(); registerReceiver(m_timeChangedReceiver, s_intentFilter); }
编辑:
并取消注册:
public void onDestroy() { super.onDestroy(); unregisterReceiver(m_timeChangedReceiver); }
除了接受的答案
如果您想在应用没有运行的情况下收听时间变化,我会在清单中注册:
<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>
如果你这样做,不要在registerReceiver
和unregisterReceiver
的代码中明确地注册接收者。
再一次,这只是对接受答案的补充。