2011-11-21 3 views
0

내 앱에서 타이머와 timerTask를 사용하여 반복적으로 특정 작업을 수행하고 있습니다. 타이머가 서비스에 사용 중입니다. 나는 특정 지연 후 TimerTask를 호출하지 않는 타이머를 실행하면 내 코드는 타이머가 제대로 작동하지 않습니다.

`

final Timer timer = new Timer(); 
public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 
     Log.i(TAG, "onStart"); 
     Thread thread = new Thread(this); 
     thread.start(); 
try { 
    Thread.sleep(GlobalClass.time); 
} catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
@Override 
    public void run() { 
     Log.i(TAG, "onStart"); 


     timer.schedule(new TimerTask() { 

      @Override 
      public void run() { 
       Log.i(TAG, "i am in timertask"); 
       try { 
        setbackground(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 
     }, GlobalClass.time, GlobalClass.time); 
    } 

는`GlobalClass.time 정적 긴 변수입니다. 도와주세요. ocCreate 반복 declear을 함수를 호출

답변

1

사용 핸들러()

Handler handler = new Handler(); 
handler.postDelayed(checkFunction(),1000); 

기능을 만들 수는이 HTTP (내 [질문]를 참조 나를 위해 작동하지 않는 경우 귀하의 요구 사항

private Runnable checkFunction(){ 
     Timer t = new Timer(); 
     TimerTask tt = new TimerTask() {    
      public void run() { 
       handler.postDelayed(checkFunction(),1000); 
       /// write coding for your requirement here 
      }   
     };   
    return tt; 
    } 
+0

에 따라 달라집니다 //stackoverflow.com/questions/14924295/service-of-an-app-not-running-when-phone-is-not-charged) 여기에 ragarding – HiB

관련 문제