2016-05-14 163 views
0

我試圖重新啓動中斷的線程在onResume(我解釋onPause中的線程)。爲此,我在網上看到很多例子,但沒有什麼是有用的(可能是我的錯)。所以,請告訴我如何重新啓動中斷的線程中的onResumeAndroid:重新啓動中斷線程onResume

我的代碼:

private void runThread(){ 

threadService = new Thread() { 

    @Override 
    public void run() { 
     try { 
      while (!isInterrupted()) { 
       Thread.sleep(1000); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 

         Log.e("Thread", "thread"); 

         if (freshMSgId != null) { 
          getPrevChatVolleyInThread(); 
         } 
        } 
       }); 
      } 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
}; 

threadService.start(); 

} 

@Override 
protected void onPause() { 
super.onPause(); 

if (threadService != null) { 
    threadService.interrupt(); 
} 
} 
+0

您不能重新啓動被中斷的線程。有一些很好的想法,你可以*在接受的答案在這裏做:http://stackoverflow.com/questions/1881714/how-to-start-stop-restart-a-thread-in-java – npace

回答

0

@npace謝謝,我接到了您的評論的想法。我重新開始中斷的線程在onResume像

@Override 
protected void onResume() { 
    super.onResume(); 
    runThread(); 
}