2016-12-29 10 views
0

내가처럼 될 것 '추구'텍스트 만들고 싶어 (안드로이드를) 제대로 작동하지 않습니다. "로드" 을 -> "로드 중 ..."-> "로드 중 ..."미래의 작업은

그런 다음 특정 데이터를 얻으면이 애니메이션 텍스트를 중지하고 텍스트를 "Found"로 설정하십시오.

나는 그런 식으로 구현하는 시도 :

ExecutorService threadPoolExecutor = Executors.newSingleThreadExecutor(); 
Future textAnimationTaskFuture; 
Runnable textAnimationTask; 

textAnimationTask = new Runnable() { 
      @Override 
      public void run() { 
       int passedTime = 0; 
       while(passedTime < 3000) 
       { 
        if(passedTime < 1000){   
         textView.setText("Loading."); 
        } 
        else if(passedTime >= 1000 && passedTime < 2000) 
        { 
         textView.setText("Loading.."); 
        }else if (passedTime >= 3000) 
        { 
         textView.setText("Loading..."); 
        } 
        passedTime = passedTime + 1; 
        if(passedTime == 3000) 
         passedTime = 0; 
       } 
      } 
     }; 

을 그리고 나서 프로세스 실행됩니다 :

textAnimationTaskFuture = threadPoolExecutor.submit(textAnimationTask); 

을 그리고 취소

textAnimationTaskFuture.cancel(true); 
textView.setText("Found.); 

불행하게도 루프하지 않는 취소시 중지 (true).

답변

0

왜 일관성이 있는지 정확히 알지 못하지만 하나의 명확한 문제는 UI 스레드가 아닌 스레드에서보기를 변경하려고한다는 것입니다. 보기에 텍스트를 설정하려면 다음을 수행해야합니다

runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      textView.setText("MY_TEXT"); 
     } 
    }); 

가 중지 루프를하지에 대해서 - 내가 루프에 문제를 감지 할 수 당신이 코드를 찾고에서, 그것을 디버깅 좋습니다.

+0

디버깅을 시도했지만 불행히도 문제를 찾을 수없는 것 같습니다. 어쩌면 내가 뭔가 잘못하고있는 것 같아요./ – BVtp

+0

흠 ... 여기를 보시면 아마도 도움이 될 것입니다. http://stackoverflow.com/questions/13623445/future-cancel-method-is-not-working – yakobom