2016-06-14 91 views
0

我想在發生特殊事件時開始計算我的服務時間。我想在工作線程中做到這一點。
甚至使用CountDownTimer()可以爲我做這個。
問題是,當我在IntentService類的OnHandleIntent()中使用此方法時,我收到一個錯誤:
java.lang.IllegalStateException: Handler (android.os.CountDownTimer$1) {235e78c} sending message to a Handler on a dead thread使用Android中的工作線程計算服務時間

這是在時間到達目的地時計算時間和做特殊工作的最佳方式嗎?如果不是我可以如何?如果是,如何解決?謝謝。

我的服務代碼是:

import android.app.IntentService; 
import android.content.Context; 
import android.content.Intent; 
import android.os.CountDownTimer; 
import android.os.Environment; 
import android.util.Log; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 

public class TimeService extends IntentService { 



    public TimeService(){ 
     super(""); 

    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 

     new CountDownTimer(20000,1000){ 

      @Override 
      public void onTick(long millisUntilFinished) { 
       System.out.println("Time remaining: "+millisUntilFinished/1000); 
      } 

      @Override 
      public void onFinish() { 
       System.out.println("done"); 
      } 
     }.start(); 

    } 



} 
+0

請添加代碼,這樣任何一個可以觀察到什麼錯 – USKMobility

+0

@USKMobility必要的代碼加入到質疑。 –

+0

而不是'CountDownTimer'這是用於另一個目的使用'AlarmManager' – pskink

回答

1

I can not recommend to you to use IntentService. You should use regular service.

當意圖服務啓動時,它創建一個新的工作線程和完成那麼他們的任務時,它終止。但在你的情況下,你正在使用Countdowntimer,這是工作線程終止後運行,所以它拋出異常。

一個解決方案,你可以嘗試:

protected void onHandleIntent(Intent intent) { 
     Looper.prepare(); 
     new CountDownTimer(20000,1000){ 

      @Override 
      public void onTick(long millisUntilFinished) { 
       System.out.println("Time remaining: "+millisUntilFinished/1000); 
      } 

      @Override 
      public void onFinish() { 
       System.out.println("done"); 
       Looper.myLooper().quit(); 
      } 
     }.start(); 
     Looper.loop() ; 
    } 
+0

不錯。我不想讓很多線程參與簡單的倒計時。這種方法是爲CPU優化的嗎? –

+0

是的,Looper循環處理程序線程和隊列中的倒數計時器。關於looper的更多細節https://blog.nikitaog.me/2014/10/11/android-looper-handler-handlerthread-i/ – USKMobility

+0

我得到這個錯誤:'由:java.lang.RuntimeException引起的:只有一個Looper可以創建每個線程' –

1
Trying using your countdown timer in Service class as 

CountDownTimer countDownTimerVariable; 

In @Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    super.onStartCommand(intent, flags, startId); 

// based on your condition 
if(condition) 
    { 
    performCountDown(); 
    } 
} 

public void performCountDown() 
{ 
     countDownTimerVariable = new CountDownTimer(remainingMillisForCountDown, 1000) { 
         @Override 
         public void onFinish() { 
          //Action for when the timer has finished. 

          Log.i("timer finish", "finished"); 
      } 

    @Override 
         public void onTick(long millisUntilFinished) { 
          //Action for every tick of the countdown. 
          // timeCalculate((millisUntilFinished/1000)); 
          Log.i("timer finish", "finished" + (millisUntilFinished/1000) + " Countdown"); 

         } 
        }; 
        countDownTimerVariable.start(); 
} 


    and in destroy of service 

     @Override 
    public void onDestroy() { 
    // TODO Auto-generated method stub 
    this.unregisterReceiver(notifyServiceReceiver); 
    super.onDestroy(); 

    if(countDownTimerVariable != null) 
     countDownTimerVariable.cancel(); 
} 
+0

沒有工作。我有同樣的錯誤。 –

+0

您正在使用服務或意向服務 –

+0

'IntentService' –