android - How to pause thread on button click? -


this code use pause thread removecallbacks here permanent kill thread process. want use in place of customhandler.removecallbacks(updatetimerthread) pause thread. may can use 'wait()andnotify()`.
how puase thread?

button2.setonclicklistener(new onclicklistener() {          @override         public void onclick(view arg0) {             // todo auto-generated method stub             customhandler.removecallbacks(updatetimerthread);             button.setvisibility(view.visible);            }     }); 

this thread

private runnable updatetimerthread = new runnable() {      int secs;      @override     public void run() {         // todo auto-generated method stub         timeinmillisecond = systemclock.uptimemillis()- starttime;         updatedtime = timeswapbuff+timeinmillisecond;         secs = (int)(updatedtime/1000);         int mins = secs/60;         secs = secs % 60;         int milliseconds = (int)(updatedtime % 10000);         textview.settext( string.format("%02d", secs));          customhandler.postdelayed(this,1000);         imageview1 = (imageview)findviewbyid(r.id.imageview4); switch(secs){             case 1:     imageview1.setbackgroundresource(r.drawable.img1);                         break;             case 2:      imageview1.setbackgroundresource(r.drawable.img2);                                  break;             case 3:     imageview1.setbackgroundresource(r.drawable.img3);                         break;             case 4:      imageview1.setbackgroundresource(r.drawable.img4);                                  break;             case 5:     imageview1.setbackgroundresource(r.drawable.img5);                         break;             case 6:      imageview1.setbackgroundresource(r.drawable.img6);                                  break;             case 7:     imageview1.setbackgroundresource(r.drawable.img7);                         break;             case 8:      imageview1.setbackgroundresource(r.drawable.img8);                                  break;             case 9:     imageview1.setbackgroundresource(r.drawable.img9);                         break;             case 10:      imageview1.setbackgroundresource(r.drawable.img66);                         button.setvisibility(view.visible);                         break;   }}; 

use boolean flag.

boolean pause = false; button2.setonclicklistener(new onclicklistener() {      @override     public void onclick(view arg0) {         // todo auto-generated method stub         customhandler.removecallbacks(updatetimerthread);         button.setvisibility(view.visible);      pause = true;      } }); private runnable updatetimerthread = new runnable() {  int secs;  @override public void run() {     // todo auto-generated method stub     timeinmillisecond = systemclock.uptimemillis()- starttime;     updatedtime = timeswapbuff+timeinmillisecond;     secs = (int)(updatedtime/1000);     int mins = secs/60;     secs = secs % 60;     int milliseconds = (int)(updatedtime % 10000);     textview.settext( string.format("%02d", secs));     if(!pause)         customhandler.postdelayed(this,1000);     imageview1 = (imageview)findviewbyid(r.id.imageview4);  }}; 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -