java - 通过广播接收器检查时间根本不起作用
问题描述
我试着让我的广播检查时间是否改变,因为我想每分钟检查一次我放这个的时间
android.intent.action.TIMEZONE_CHANGED android.intent.action.TIME_SET android.intent.action.TIME_TICK
但它不起作用我能做什么。
解决方案
//use these intent filter
s_intentFilter = new IntentFilter();
s_intentFilter.addAction(Intent.ACTION_TIME_TICK);
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_TICK)) {
// your work
}
}
};
//注册接收者:
public void onCreate()/onResume {
super.onCreate();
registerReceiver(m_timeChangedReceiver, s_intentFilter);
}
// 并取消注册:
public void onDestroy()/onPause {
super.onDestroy();
unregisterReceiver(m_timeChangedReceiver);
}
推荐阅读
- asp.net-core - 将文件保存到另一个系统驱动器的 API POST 方法引发 500 错误
- java - 时区不匹配
- java - 如何使用 java spark 展平嵌套的镶木地板
- java - fortify 正在为代码提供代码正确性双重检查锁定问题
- excel - 从 Excel 中的范围中提取唯一值
- javafx - 如何从 javafx 控制器而不是 CSS 向按钮添加悬停属性
- python - 使用 Python 将参数传递给 AutoIt 程序
- html - 带有 #anchor 标记和查询参数的 URL
- mysql - 需要在 sql 表中查找与某组订单 ID 相对应的发货 ID 计数,但查询有问题
- math - 圆曲线角度