How to pause/resume thread in Android?

I have a thread that running into an activity. I don’t want that the thread continuos running when the user click the home button or, for example, the user receive a call phone.
So I want pause the thread and resume it when the user re-opens the application.
I’ve tried with 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 the thread but don’t resume it, the thread seems freezed.

  • Why does Cordova/Phonegap append 8 to my Android Version Code?
  • Android RecyclerView : notifyDataSetChanged() IllegalStateException
  • Android Tablet - Get Unique Device ID
  • How To Add Second Activity In Android
  • Failed binder transaction when putting an bitmap dynamically in a widget
  • Android disable listview items
  • I’ve also tried with the deprecated method Thread.suspend() and Thread.resume(), but in this case into Activity.onPause() the thread doesn’t stop.

    Anyone know the solution?

    Related posts:

    Need to disable expand on CollapsingToolbarLayout for certain fragments
    ViewPager works Fine at first time but on reloading again, getting the error java.lang.IllegalStateE...
    Disable sleep on certain activity
    Android imageview change tint to simulate button click
    java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10210 nor current process.. in ...
    Why do I get NullPointerException when sending an SMS on an HTC Desire, or what is SubmitPdu?
  • Why getApplicationContext().setTheme() in a Activity does not work?
  • Align the child views in center of the ViewPager android
  • Android: Samsung Galaxy S3 screen size for layout
  • ViewPager fragments disappear when change screen rotation
  • How to store modulus, public exponent and private exponent securely on Android?
  • Best practice to use Sprites in a game using AndEngine GLES2
  • 2 Solutions collect form web for “How to pause/resume thread in Android?”

    Use wait() and notifyAll() properly using a 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) {
                // Do stuff.
    
                synchronized (mPauseLock) {
                    while (mPaused) {
                        try {
                            mPauseLock.wait();
                        } catch (InterruptedException e) {
                        }
                    }
                }
            }
        }
    
        /**
         * Call this on pause.
         */
        public void onPause() {
            synchronized (mPauseLock) {
                mPaused = true;
            }
        }
    
        /**
         * Call this on resume.
         */
        public void onResume() {
            synchronized (mPauseLock) {
                mPaused = false;
                mPauseLock.notifyAll();
            }
        }
    
    }
    

    Try the below code it will work

    Thread thread=null;
    

    OnResume()

      public void onResume(){
      super.onResume();
      if(thread == null){
      thread = new Thread()
      {
          @Override
          public void run() {
              try {
    
                  }
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      };
    
      thread.start();
          }
      }
    

    onPause()

    @Override
     public void onPause(){
    super.onPause();
    if(thread != null){
    Thread moribund = thread;
    thread = null;
    moribund.interrupt();
    }
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.