Android:小部件没有保存倒计时变量

2022-10-15 03:10:49标签androidwidgetandroid-widget
提问

我正在制作一个Android小部件,当我点击一个按钮(通过意图TAG_WAIT2_CLICK来表示)时,它会启动倒计时。 我想要按钮,不要在现在的倒计时结束前启动新的倒计时。 问题是,每次我点击按钮时,可变倒计时仍然是空的,因此launch_timer就会实现,因此启动了新的倒计时。 你知道为什么吗? 一旦您从onReceive()返回任何显表注册的广播接收机,如您的AppWidgetProvider被丢弃,并将被收集。此外,如果没有其他任何东西使你的过程(例如前台服务),你的过程将很快被终止,以释放其他应用程序的系统RAM。

public class AlphaProvider extends AppWidgetProvider {
    private CountDown countDown;
    @Override
    public void onReceive(Context paramContext, Intent paramIntent)
    {
        boolean launch_timer;
        if (paramIntent.getAction().equals(TAG_WAIT2_CLICK)) {
            if (countDown == null) launch_timer = true;
            else if (!countDown.isTicking()) launch_timer = true;
            else launch_timer = false;
            if (launch_timer) {
                String str = paramIntent.getAction();
                RemoteViews localRemoteViews = buildUpdate(paramContext, str);
                ComponentName localComponentName = new ComponentName(paramContext, AlphaProvider.class);
                AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews);
                countDown = new CountDown(6000, 1000, rview, paramContext, R.id.progressBar_Wait2, R.id.textViewWait2);
                countDown.start();                    
            }
        }
        else
        {
            super.onReceive(paramContext, paramIntent);
        }
    }
}    
public class CountDown extends CountDownTimer {
    private RemoteViews views;
    private Context paramContext;
    private int secondsMax;
    private int ProgressBarId;
    private int TextViewId;
    private boolean isTicking;
    public CountDown(long millisInFuture, int countDownInterval, RemoteViews views, Context paramContext, int ProgressBarId, int TextViewId) {
        super(millisInFuture, countDownInterval);
        this.views = views;
        this.paramContext = paramContext;
        this.secondsMax = (int) (millisInFuture / 1000);
        this.ProgressBarId = ProgressBarId;
        this.TextViewId = TextViewId;
        this.isTicking = false;
    }
    @Override
    public void onTick(long millisUntilFinished) {
        int secondsLeft = (int) (millisUntilFinished / 1000);
        views.setProgressBar(ProgressBarId,this.secondsMax,secondsLeft,false);
        if (secondsLeft<=0)
        {
            isTicking = false;
        } else {
            isTicking = true;
        }
        ComponentName localComponentName = new ComponentName(paramContext, AlphaProvider.class);
        AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, views);
    }
    public boolean isTicking() { return isTicking; }
}
回答

▼版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说cuoshuo.com——程序员的报错记录

部分内容根据CC版权协议转载,如果您希望取消转载请发送邮件到cuoshuo8@163.com

辽ICP备19011660号-5