2017-04-22 65 views
-1

嗨,我是新的android和在我的應用程序我使用進度條爲此錄音爲此,當我開始錄製我正在更新進度欄,這很好,在這裏,當我停止錄音進度沒有停止它的連續更新,請幫我有人如何停止進度,當我停止錄音如何停止進度條,當我們停止處理程序android

private void startRecording() { 
    try { 
     handler = new Handler(); 
     recorderProgressBar = (ProgressBar) dialog.findViewById(R.id.recorder_progressBar); 
     progressStatus = 0; 
     // Start the lengthy operation in a background thread 
     thread = new Thread() { 
      @Override 
      public void run() { 
       while (progressStatus < 100) { 
        // Update the progress bar 
        if (handler != null) { 
         // Update the progress status 
         progressStatus += 1; 
         // Try to sleep the thread for 20 milliseconds 
         try { 
          Thread.sleep(3000); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
         handler.post(runnable); 
        } 
       } 
      } 
     }; 
     thread.start(); 
    } catch (Throwable throwable) { 
     throwable.printStackTrace(); 
    } 
} 

Runnable runnable = new Runnable() { 
    public void run() { 
     System.out.println("it's calling ramakrishna"); 
     recorderProgressBar.setProgress(progressStatus); 
    } 
}; 

private void stopRecording() { 

    //Kill Background thread 
    if (handler != null) { 
     handler.removeCallbacks(runnable); 
    } 
} 
+0

OK,讓我試着告訴你導致 – Krish

+0

沒有連我初始化處理程序時活動開始進度更新 – Krish

+0

請讓我知道solutiuon – Krish

回答

0

您可以停止線程來代替。

private void stopRecording() { 
    //Kill Background thread 
    if (handler != null) { 
     handler.removeCallbacks(runnable); 
    } 

    if (thread != null) { 
     thread.stop(); 
    } 
} 

或者你可以使用thread.interrupt()的建議在這裏:Do not use Thread.stop()

+0

UnsupportedOperationException異常的解決方案我收到 – Krish

+0

沒有用,即使我使用thread.interrupt() – Krish

+0

我的答案中的鏈接顯示了3種停止線程的方式,沒有什麼適合你的? –

0

定義recorderProgressBar出該方法,然後停止,如果它不爲空。