2016-07-24 102 views
0

这个简单的Runnable其中我可以postDelay开始和removeCallbacks停止。但我不喜欢那样。Android暂停线程而不是删除并停止

我想暂停handler而不是停止removeCallbacks,然后重新开始恢复暂停处理程序,如播放/暂停音乐。可能吗?

/* START */ 
protected final Runnable mUpdateUI = new Runnable() { 
    public void run() { 
     displayData(); 
     mHandler.postDelayed(mUpdateUI, 5000); 
    } 
}; 

/* STOP */ 
customHandler.removeCallbacks(updateTimerThread); 

或其他解决方案播放和暂停线程。

回答

0

试试这个,因为就像在音乐的暂停暂停不适:

public class LooperThread extends Thread { 

    private Handler mHandler; 

    public void run() { 
     Looper.prepare(); 

     synchronized (this) { 
      mHandler = new Handler() { 
       displayData(); 
      }; 
      notifyAll(); 
     } 

     Looper.loop(); 
    } 

    public synchronized Handler getHandler() { 
     while (mHandler == null) { 
      try { 
       wait(); 
      } catch (InterruptedException e) { 
       //Ignore and try again. 
      } 
     } 
     return mHandler; 
    } 
}