2017-02-24 8 views
-1

タイマーサービスに問題があります。アクティビティを終了するたびに、サービスはカウントダウンを続ける代わりにカウントダウンを再開します。私はそれがアプリケーションを終了してもカウントダウンを続行したい。タイマーがカウントダウンを続ける

public class TimerService extends Service { 
CountDownTimer timer; 
Handler mHandler = new Handler(); 
private static final String TAG="com.timer"; 
@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

public void onCreate(){ 
    mHandler.post(new Runnable(){ 

     @Override 
     public void run() { 
      timer = new CountDownTimer(20000, 1000) { 
       @Override 
       public void onFinish(){ 

       } 

       @Override 
       public void onTick(long millisUntilFinished) { 

        Log.i(TAG,"" + millisUntilFinished/1000); 

       } 
     }; 
     timer.start(); 
     } 

    }); 

} 
public int onStartCommand(Intent intent, int flags, int startId){ 


    return super.onStartCommand(intent, flags, startId); 
} 

public void onDestroy(){ 
    timer.cancel(); 
    super.onDestroy(); 
} 

}

+0

'onStartCommand'から' START_NOT_STICKY'を返してみてください。 –

+0

同じことが起こります –

答えて

0

あなたはタイマーを終了したい場合は、onFinish()空で

timer.cancel(); 

メソッドを呼び出す必要があります。 これはタイマーを終了させます。

関連する問題