本文介绍了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"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical" > <TextVIEw androID:ID="@+ID/mytextvIEw" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:text="@string/number" /> <linearLayout androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:orIEntation="horizontal" > <button androID:ID="@+ID/mybutton_start" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/start" /> <button androID:ID="@+ID/mybutton_pause" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/pause" /> </linearLayout></linearLayout>
string部分代码:
<?xml version="1.0" enCoding="utf-8"?><resources> <string name="app_name">TimerDemo</string> <string name="number">0</string> <string name="start">start</string> <string name="stop">stop</string> <string name="pause">pause</string> <string name="resume">resume</string></resources>
上面就是我的源代码,如果大家有什么问题可以留言进行探讨。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android定时器Timer的停止和重启实现代码全部内容,希望文章能够帮你解决Android定时器Timer的停止和重启实现代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)