java - Pause and Continue a Runnable object? -


i have implemented runnable in code, , have start pressing button in app, , stop pressing button.

the problem occurs when press stop button. stops runnable, can't start again, when press start button.

my code:

public void onclick(final view v) {     switch (v.getid()) {     case r.id.button_timerstart:         log.e("mainactivity", "clicked");         r = new runnable() {             public void run() {                 if (mservice != null) {                     if(running) {                         str2 = ef.gettext().tostring();                         str2 = str2.substring(0, 0) + "e" + str2.substring(0, str2.length());                         mservice.sendalert(mdevice, str2);                         v.postdelayed(r, 6 * 1000);                     }                 }             }         };         v.post(r);         break;     case r.id.button_timerstop:         log.e("mainactivity", "clicked");         if(running = true) {             running = false;         }     default:         log.e(tag,"wrong click event");         break;     } } 

don't use running flag signify want stop it.

instead use removecallbacks() remove runnables the list played.

/to stop runnable: v.removecallbacks(r); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -