2017-04-22 2 views
-1

안녕하세요 저는 Android를 처음 사용하고 있고 내 앱에서 녹음을 시작할 때 진행률 표시 줄을 업데이트 할 때 진행률 표시 줄을 사용하고 있습니다. 내가 진행 막대 녹음을 중지 여기 때 연속적으로 몇 가지 중 하나가 내가우리가 android에서 Handler를 멈추었을 때 진행 막대를 멈추는 방법

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

확인 나 시도하고 활동 – Krish

+0

더 심지어 내가 나를 – Krish

+0

를 업데이트 ProgressBar를 시작할 때 – 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가와 솔루션을 제공하시기 바랍니다하고 핸들러를 초기화하지 않습니다 당신이 결과를 말해 보자 thread.interrupt() – Krish

+0

을 받고하지하고 나에게 코드 – Krish

+0

내 대답의 링크는 스레드를 중지하는 3 가지 방법을 보여줍니다. 아무 것도 작동하지 않습니까? –

0

메소드에서 recorderProgressBar를 정의한 다음 null이 아닌 경우 중지하십시오.

관련 문제