Android停止CountDown

时间:2012-04-19 21:06:30

标签: android widget countdown

我有一个显示倒计时的小部件。为了做到这一点,我使用CountDownTimer类,但问题是倒计时经常停止!我认为因为android会自动停止线程因为countdownc在很多小时内。 HOw可以解决这个问题吗?谢谢

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);
            };
        }

1 个答案:

答案 0 :(得分: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);