本文介绍了Android定时器Timer的停止和重启实现代码,分享给大家,具体如下:
员工经过长期磨合与沉淀,具备了协作精神,得以通过团队的力量开发出优质的产品。创新互联坚持“专注、创新、易用”的产品理念,因为“专注所以专业、创新互联网站所以易用所以简单”。公司专注于为企业提供成都网站设计、网站制作、微信公众号开发、电商网站开发,微信小程序,软件按需设计等一站式互联网企业服务。
7月份做了一个项目,利用自定义控件呈现一幅动画,当时使用定时器来控制时间,但是当停止开启时总是出现问题。一直在寻找合理的方法解决这个问题,一直没有找到,最近终于找到了合理的方法来解决这个问题。
大家如何查询有关资料,一定知道timer,timertask取消的方式是采用Timer.cancel()和mTimerTask.cancel(),可是大家发现这种发式取消后,再次开始timer时,会报错
FATAL EXCEPTION: main Process: com.example.zhongzhi.gate_control_scheme, PID: 2472 java.lang.IllegalStateException: Timer already cancelled. at java.util.Timer.sched(Timer.java:397) at java.util.Timer.schedule(Timer.java:248) at com.example.zhongzhi.gate_control_scheme.MainActivity.onClick(MainActivity.java:401) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这个问题的解决采用cancle(),取消timer后,还需要清空timer。合理的代码应该是这样的:
mTimer.cancel(); mTimer = null; mTimerTask.cancel(); mTimerTask = null;
关键的问题解决完了,下面给出我的案例代码Mainactivity.Java:
public class MainActivity extends AppCompatActivity { private static String TAG = "TimerDemo"; private TextView mTextView = null; private Button mButton_start = null; private Button mButton_pause = null; private Timer mTimer = null; private TimerTask mTimerTask = null; private Handler mHandler = null; private static int count = 0; private boolean isPause = false; private boolean isStop = true; private static int delay = 1000; //1s private static int period = 1000; //1s private static final int UPDATE_TEXTVIEW = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView)findViewById(R.id.mytextview); mButton_start = (Button)findViewById(R.id.mybutton_start); mButton_pause = (Button)findViewById(R.id.mybutton_pause); mButton_start.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (isStop) { Log.i(TAG, "Start"); } else { Log.i(TAG, "Stop"); } isStop = !isStop; if (!isStop) { startTimer(); }else { stopTimer(); } if (isStop) { mButton_start.setText(R.string.start); } else { mButton_start.setText(R.string.stop); } } }); mButton_pause.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (isPause) { Log.i(TAG, "Resume"); } else { Log.i(TAG, "Pause"); } isPause = !isPause; if (isPause) { mButton_pause.setText(R.string.resume); } else { mButton_pause.setText(R.string.pause); } } }); mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case UPDATE_TEXTVIEW: updateTextView(); break; default: break; } } }; } private void updateTextView(){ mTextView.setText(String.valueOf(count)); } private void startTimer(){ if (mTimer == null) { mTimer = new Timer(); } if (mTimerTask == null) { mTimerTask = new TimerTask() { @Override public void run() { Log.i(TAG, "count: "+String.valueOf(count)); sendMessage(UPDATE_TEXTVIEW); do { try { Log.i(TAG, "sleep(1000)..."); Thread.sleep(1000); } catch (InterruptedException e) { } } while (isPause); count ++; } }; } if(mTimer != null && mTimerTask != null ) mTimer.schedule(mTimerTask, delay, period); } private void stopTimer(){ if (mTimer != null) { mTimer.cancel(); mTimer = null; } if (mTimerTask != null) { mTimerTask.cancel(); mTimerTask = null; } count = 0; } public void sendMessage(int id){ if (mHandler != null) { Message message = Message.obtain(mHandler, id); mHandler.sendMessage(message); } } }
xml部分代码:
<?xml version="1.0" encoding="utf-8"?>
string部分代码:
<?xml version="1.0" encoding="utf-8"?>TimerDemo 0 start stop pause resume
上面就是我的源代码,如果大家有什么问题可以留言进行探讨。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。