2012-04-19 118 views
0

我有一個顯示倒計時的小部件。爲了做到這一點,我使用CountDownTimer類,但問題是倒計時經常停止!我認爲,因爲android自動停止線程,因爲在很多houres期間countdownc。你可以解決這個問題嗎?謝謝Android stop CountDown

public class TempoIndietro extends CountDownTimer{ 
    AppWidgetManager manager; 
    ComponentName thisWidget; 

    public TempoIndietro(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
     thisWidget = new ComponentName(context, widget.class); 
     manager = AppWidgetManager.getInstance(context); 
     remoteView = new RemoteViews(context.getPackageName(),R.layout.widgett); 
    } 

    @Override 
    public void onFinish() { 
    remoteView.setTextViewText(R.id.textView2, context.getResources().getString(R.string.onair_widget_countdown)); 
     manager.updateAppWidget(thisWidget, remoteView); 
      } 

      @Override 
      public void onTick(long millisUntilFinished) { 
       SimpleTimeFormat tf = new SimpleTimeFormat("$dd$ : $HH$: $mm$: $ss$");  
       String risultato = tf.format(millisUntilFinished); // arg0 tempo 
       remoteView.setTextViewText(R.id.textView2, risultato); 
       manager.updateAppWidget(thisWidget, remoteView); 
      }; 
     } 
+0

怎麼是你應該來幫助你看到你的代碼 – slayton 2012-04-19 21:09:26

+0

我有加入倒數代碼到答案 – MimmoG 2012-04-19 22:10:35

+0

當onTick()執行的呢?換句話說,何時更新小部件?在每一分鐘? – San 2012-04-19 22:53:24

回答

0

請記住您的計時器的開始時間,並計算與此時間的差異。

您可以使用本地存儲。

最好在Activity的onStop()和onResume()中調用它。

節省:

SharedPreferences settings = getSharedPreferences("myappname", 0); 
SharedPreferences.Editor editor = settings.edit(); 
editor.putLong("varname", startDate.getTime()); 
editor.commit(); 

檢索:

SharedPreferences settings = getSharedPreferences("myappname", 0); 
Long millis = settings.getLong("varname", null); 
Date startDate = new Date(millis); 
+0

這是一個小部件的問題是,這個倒計時可以在很多小時或幾天和android殺死倒計時線程.... – MimmoG 2012-04-20 03:16:54