在我的应用程序中,有一个警报服务,我发现如果用户将其日期或时间更改为过去的时间.在我预期的时候我的警报不会被触发.
因此,我可能需要再次重置所有警报.在android中是否有日期和时间更改监听器?
Ben*_*ish 89
创建一个意图过滤器:
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);
}
Run Code Online (Sandbox Code Playgroud)
和广播接收器:
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();
}
}
};
Run Code Online (Sandbox Code Playgroud)
注册接收者:
public void onCreate() {
super.onCreate();
registerReceiver(m_timeChangedReceiver, s_intentFilter);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
并取消注册:
public void onDestroy() {
super.onDestroy();
unregisterReceiver(m_timeChangedReceiver);
}
Run Code Online (Sandbox Code Playgroud)
Cha*_*ham 27
如果您想在应用未运行时收听时间更改,我会在清单中注册:
<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>
Run Code Online (Sandbox Code Playgroud)
如果这样做,请不要使用registerReceiver和在代码中明确注册接收器unregisterReceiver.
同样,这只是对已接受答案的补充.
| 归档时间: |
|
| 查看次数: |
47197 次 |
| 最近记录: |