multithreading - How to pause/resume thread in Android? -


i have thread running activity. don't want thread continuos running when user click home button or, example, user receive call phone. want pause thread , resume when user re-opens application. i've tried this:

protected void onpause() {   synchronized (thread) {     try {       thread.wait();     } catch (interruptedexception e) {       e.printstacktrace();     }   }   super.onpause(); } protected void onresume() {   thread.notify();   super.onresume(); } 

it stops thread don't resume it, thread seems freezed.

i've tried deprecated method thread.suspend() , thread.resume(), in case activity.onpause() thread doesn't stop.

anyone know solution?

use wait() , notifyall() using lock.

sample code:

class yourrunnable implements runnable {     private object mpauselock;     private boolean mpaused;     private boolean mfinished;      public yourrunnable() {         mpauselock = new object();         mpaused = false;         mfinished = false;     }      public void run() {         while (!mfinished) {             // stuff.              synchronized (mpauselock) {                 while (mpaused) {                     try {                         mpauselock.wait();                     } catch (interruptedexception e) {                     }                 }             }         }     }      /**      * call on pause.      */     public void onpause() {         synchronized (mpauselock) {             mpaused = true;         }     }      /**      * call on resume.      */     public void onresume() {         synchronized (mpauselock) {             mpaused = false;             mpauselock.notifyall();         }     }  } 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

php - How can I edit my code to echo the data of child's element where my search term was found in, in XMLReader? -