停止 运行 处理程序
Stopping a running handler
我实现了一个处理程序,允许用户使用任务栏菜单启动一个每 5 秒重复一次的事件。允许用户从任务栏菜单停止事件的最佳使用方法是什么?
public class MainActivity extends Activity{
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menuitem_tracklogOn:
final Handler handler = new Handler();
Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
//DO SOME ACTIONS HERE , THIS ACTIONS WILL WILL EXECUTE AFTER 5 SECONDS...
handler.postDelayed(this, 5000);//repeat every n-milliseconds
}
});
}
}, 0);//start after n-milliseconds
return true;
case R.id.menuitem_tracklogOff:
//stops running event from R.id.menuitem_tracklogOn
return true;
解决方案:
final Handler handler = new Handler();
Timer t = new Timer();
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menuitem_tracklogOn:
t.schedule(new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
//DO SOME ACTIONS HERE , THIS ACTIONS WILL WILL EXECUTE AFTER 5 SECONDS...
handler.postDelayed(this, 5000);//repeat every n-milliseconds
}
});
}
}, 2000);//start after n-milliseconds
return true;
case R.id.menuitem_tracklogOff:
t.cancel();
handler.removeCallbacksAndMessages(null);
return true;
您需要致电 timer.cancel()
取消定时器。
public void cancel () // Added in API level 1
Cancels the Timer and all scheduled tasks. If there is a currently running task it is not affected. No more tasks may be scheduled on this Timer. Subsequent calls do nothing.
What is the best method to use that would also allow the user to stop
the event from the taskbar menu?
要在 menuitem_tracklogOff
上停止 Handler
,请单击:
1. 通过调用 timer.cancel()
取消 Timer
2.调用removeCallbacksAndMessages
删除所有回调和消息:
handler.removeCallbacksAndMessages(null);
首先要访问 Timer
和 Handler
对象的外部 case
声明其他外部 onOptionsItemSelected 方法或在 switch-case
块之前。
我实现了一个处理程序,允许用户使用任务栏菜单启动一个每 5 秒重复一次的事件。允许用户从任务栏菜单停止事件的最佳使用方法是什么?
public class MainActivity extends Activity{
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menuitem_tracklogOn:
final Handler handler = new Handler();
Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
//DO SOME ACTIONS HERE , THIS ACTIONS WILL WILL EXECUTE AFTER 5 SECONDS...
handler.postDelayed(this, 5000);//repeat every n-milliseconds
}
});
}
}, 0);//start after n-milliseconds
return true;
case R.id.menuitem_tracklogOff:
//stops running event from R.id.menuitem_tracklogOn
return true;
解决方案:
final Handler handler = new Handler();
Timer t = new Timer();
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menuitem_tracklogOn:
t.schedule(new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
//DO SOME ACTIONS HERE , THIS ACTIONS WILL WILL EXECUTE AFTER 5 SECONDS...
handler.postDelayed(this, 5000);//repeat every n-milliseconds
}
});
}
}, 2000);//start after n-milliseconds
return true;
case R.id.menuitem_tracklogOff:
t.cancel();
handler.removeCallbacksAndMessages(null);
return true;
您需要致电 timer.cancel()
取消定时器。
public void cancel () // Added in API level 1
Cancels the Timer and all scheduled tasks. If there is a currently running task it is not affected. No more tasks may be scheduled on this Timer. Subsequent calls do nothing.
What is the best method to use that would also allow the user to stop the event from the taskbar menu?
要在 menuitem_tracklogOff
上停止 Handler
,请单击:
1. 通过调用 timer.cancel()
Timer
2.调用removeCallbacksAndMessages
删除所有回调和消息:
handler.removeCallbacksAndMessages(null);
首先要访问 Timer
和 Handler
对象的外部 case
声明其他外部 onOptionsItemSelected 方法或在 switch-case
块之前。