我有一个显示倒计时的小部件。为此,我使用了 CountDownTimer 类,但问题是倒计时经常停止!我认为是因为 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);
};
}