Timer.cancel()无效。Android长按按钮一定时间执行某个操作。
2017-04-13 12:59
561 查看
在timer 中 timerTask执行过程中cancel是无效的。
查看源码:
要想停止任务可以使timerTask 取消
eg:
查看源码:
/** * Cancels the {@code TimerTask} and removes it from the {@code Timer}'s queue. Generally, it * returns {@code false} if the call did not prevent a {@code TimerTask} from running at * least once. Subsequent calls have no effect. * * @return {@code true} if the call prevented a scheduled execution * from taking place, {@code false} otherwise. */ public boolean cancel() { synchronized (lock) { boolean willRun = !cancelled && when > 0; cancelled = true; return willRun; } }
要想停止任务可以使timerTask 取消
eg:
llBlank.setOnTouchListener(new View.OnTouchListener() { private TimerTask task; private Timer mTimer; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mTimer = new Timer(); task = new TimerTask() { @Override public void run() { Message message = new Message(); Vibrator vib = (Vibrator) getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE); long[] pattern = {0, 80}; if (rlAdvance.getVisibility() == View.GONE) { /**震动服务*/ vib.vibrate(pattern, 1); message.what = showAdvance; handler1.sendMessage(message); } } }; mTimer.schedule(task, 2000); break; case MotionEvent.ACTION_UP: mTimer.cancel(); task.cancel(); } return false; } } ); }
相关文章推荐
- 页面鼠标键盘在一定时间内无操作自动跳转(或者执行相应的业务)
- Android关于界面一定时间无操作自动跳转到指定界面的实现
- Android设置特定时间执行操作
- Android 指定日期时间执行任务的Timer
- Android每隔一定时间就重复执行某段代码
- android 一定间隔时间后执行操作,如防止重复点击、5s后弹出对话框
- Spring之任务调度(类似javascript的timeout方法,每隔一定时间执行操作)
- Android关于界面一定时间无操作自动跳转到指定界面的实现
- Android Button按钮点击两次之后,才执行点击事件的操作。
- 解决android 长按语音操作 弹出语音层 点击其他的按钮会影响语音层的时间
- 1.利用android提高的的insert,query,update,deleteAPI与execSql,rawQuery函数执行原生的插入,查询,更新,删除语句操作花费时间的对比结果
- 在Android中使用Handler和Thread线程执行后台操作
- 在Android 的Camera 预览上执行 Drawing 操作
- 异常信息:CLR无法从COM 上下文0x645e18 转换为COM上下文0x645f88,这种状态已持续60秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作.这种情况通常会影响到
- oracle 等待一定时间执行下面的语句
- Android---AlarmManager(全局定时器/闹钟)指定时长或以周期形式执行某项操作(转http://www.cnblogs.com/jico/archive/2010/11/03/1868361.html)
- Android---AlarmManager(全局定时器/闹钟)指定时长或以周期形式执行某项操作
- android按钮的操作例子,简单大家看明白_基础篇
- android按钮的操作例子,简单大家看明白_基础篇
- oracle 中--怎么查看当前表空间在作什么操作?--查锁,死锁,当前执行时间长的Sql语句,没提交的事务,对象为哪些进程所用