How can I restart a thread in java/Android from a button? -
i have thread in java/android this:
handler handler = new handler() { @override public void handlemessage(message msg) { // todo auto-generated method stub update_i(); } }; @override protected void onstart() { // todo auto-generated method stub super.onstart(); thread mythread = new thread(new runnable() { public void run() { while (true) { try { handler.sendmessage(handler.obtainmessage()); thread.sleep(timer); } catch (throwable t) { } } } }); mythread.start(); }
the thread works fine when run application. want start/restart thread button.
button.onclicklistener startbuttononclicklistener = new button.onclicklistener() { @override public void onclick(view v) { //start/restart thread } };
if copy thread button make new thread every time user clicks on button. want run thread when user first time click on button, "kill it" , start beginning if user click on button second time (i don’t want start second thread).
you can't restart thread.
from documentation :
throws illegalthreadstateexception if thread has been started before
you can kill previous thread, in end have create second instance of thread.
resources :
Comments
Post a Comment