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

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -