如何在android上每隔1秒使用一次命令intentfilter?
我要每1秒注册一次 我试着如何在android上每隔1秒使用一次命令intentfilter?,android,Android,我要每1秒注册一次 我试着 registerReceiver(receiver, new IntentFilter(Intent.ACTION_TIME_TICK)); 但这段代码每1分钟更新一次 我想要每一秒 也许,android有一个表单 谢谢你想完成什么?如果您只想每隔1秒执行一些代码,请不要使用BroadcastReceiver。接收器每次触发时都会导致进程间通信,这是(相对)昂贵的 最好的方法是使用处理程序 private static final long TICK_INTERVA
registerReceiver(receiver, new IntentFilter(Intent.ACTION_TIME_TICK));
但这段代码每1分钟更新一次
我想要每一秒
也许,android有一个表单
谢谢你想完成什么?如果您只想每隔1秒执行一些代码,请不要使用
BroadcastReceiver
。接收器每次触发时都会导致进程间通信,这是(相对)昂贵的
最好的方法是使用处理程序
private static final long TICK_INTERVAL = TimeUnit.SECONDS.toMillis(1);
private static final Handler tickHandler = new Handler(Looper.getMainLooper());
public void onResume() {
super.onResume();
tick(TICK_INTERVAL);
}
private void tick(final long interval) {
tickHandler.postDelayed(
new Runnable() {
public void run() {
tick(interval);
onTick();
}
},
);
}
protected void onTick() {
// do something
}
确保在活动暂停时停止滴答声
public void onPause() {
super.onPause();
tickHandler.removeCallbacksAndMessages(null);
}
你想完成什么?如果您只想每隔1秒执行一些代码,请不要使用
BroadcastReceiver
。接收器每次触发时都会导致进程间通信,这是(相对)昂贵的
最好的方法是使用处理程序
private static final long TICK_INTERVAL = TimeUnit.SECONDS.toMillis(1);
private static final Handler tickHandler = new Handler(Looper.getMainLooper());
public void onResume() {
super.onResume();
tick(TICK_INTERVAL);
}
private void tick(final long interval) {
tickHandler.postDelayed(
new Runnable() {
public void run() {
tick(interval);
onTick();
}
},
);
}
protected void onTick() {
// do something
}
确保在活动暂停时停止滴答声
public void onPause() {
super.onPause();
tickHandler.removeCallbacksAndMessages(null);
}
时间刻度
广播仅每分钟发送一次。你不能改变这一点。请改用中所述的选项之一。仅每分钟发送一次TIME\u TICK
广播。你不能改变这一点。请改用中所述的选项之一。