Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓停止倒计时_Android_Widget_Countdown - Fatal编程技术网

Android 安卓停止倒计时

Android 安卓停止倒计时,android,widget,countdown,Android,Widget,Countdown,我有一个显示倒计时的小部件。为了做到这一点,我使用了类倒计时,但问题是,倒计时通常是停止的!我想是因为android会自动停止线程,因为倒计时会持续很多小时。如何解决这个问题?谢谢 public class TempoIndietro extends CountDownTimer{ AppWidgetManager manager; ComponentName thisWidget; public TempoIndietro(long millisInFuture,

我有一个显示倒计时的小部件。为了做到这一点,我使用了类倒计时,但问题是,倒计时通常是停止的!我想是因为android会自动停止线程,因为倒计时会持续很多小时。如何解决这个问题?谢谢

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

记住计时器的开始时间,并计算与该时间的差值

您可以为此使用本地存储

最好在活动中的onStop()和onResume()中调用它

保存:

检索:

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

您应该如何帮助您查看代码我已经在应答中添加了倒计时代码onTick()何时执行?或者换句话说,什么时候更新小部件?在每一分钟?这是一个小部件的问题是,这个倒计时可以在许多小时或几天内和安卓杀死倒计时线程。。。。
SharedPreferences settings = getSharedPreferences("myappname", 0);
Long millis = settings.getLong("varname", null);
Date startDate = new Date(millis);