gpt4 book ai didi

android - Android中的日期和时间更改监听器?

转载 作者:IT老高 更新时间:2023-10-28 13:20:54 33 4
gpt4 key购买 nike

在我的应用程序中,有一个警报服务,我发现如果用户将它的日期或时间更改为过去的时间。我的闹钟不会在我预期的时间触发。

所以,我可能不得不再次重置所有警报。 android中是否有日期和时间更改监听器?

最佳答案

创建一个 Intent 过滤器:

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);
}

关于android - Android中的日期和时间更改监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5481386/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com