在AppWidgetProvider实现CountDownTimer [英] Implement CountDownTimer in a AppWidgetProvider

查看:178
本文介绍了在AppWidgetProvider实现CountDownTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现在一个窗口小部件CountDownTimer。我想出了是:

 进口android.content.Intent;
进口android.os.CountDownTimer;
进口android.widget.RemoteViews;
进口android.widget.TextView;公共类倒计时扩展CountDownTimer {
    私人RemoteViews意见;    公共COUNTDOWN(长millisInFuture,诠释countDownInterval,RemoteViews意见){
        超(mil​​lisInFuture,countDownInterval);        this.views =意见;
    }    @覆盖
    公共无效onFinish(){
    }    @覆盖
    公共无效onTick(长millisUntilFinished){
        INT天数=(INT)(millisUntilFinished /(1000 * 60 * 60 * 24));            views.setTextViewText(R.id.countWidget,天);
    }
}

而AppWidgetProvider类

 进口java.util.Calendar中;
进口java.util.Date;
进口java.util.TimeZone中;进口android.app.PendingIntent;
进口android.appwidget.AppWidgetManager;
进口android.appwidget.AppWidgetProvider;
进口android.content.Context;
进口android.content.Intent;
进口android.widget.RemoteViews;
进口android.widget.TextView;公共类CountDownWidgetProvider扩展AppWidgetProvider {
    私人日历的currentdate = Calendar.getInstance();
    私人日历firstDecember = Calendar.getInstance();    公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
        日期日期=新的日期();        this.firstDecember.setTimeZone(TimeZone.getTimeZone(欧洲/柏林));
        this.currentDate.setTimeZone(TimeZone.getTimeZone(欧洲/柏林));
        this.firstDecember.set(2012,11,1,0,0,0); // TODO:设置为月
        this.currentDate.setTime(日期);        最终诠释N = appWidgetIds.length;        //执行的每个应用程序窗口小部件属于此提供这一循环过程
        的for(int i = 0; I< N;我++){
            INT appWidgetId = appWidgetIds [I]            //创建一个Intent推出MainActivity
            意向意图=新意图(背景下,MainActivity.class);
            的PendingIntent的PendingIntent = PendingIntent.getActivity(上下文,0,意图,0);            //获取为App插件的布局并附加上单击监听器
            //到按钮
            RemoteViews意见=新的RemoteViews(context.getPackageName(),R.layout.count_down_app_widget);
            views.setOnClickPendingIntent(R.id.countWidget,的PendingIntent);            长米利斯= this.firstDecember.getTimeInMillis() - this.currentDate.getTimeInMillis();            倒计时countDown方法=新COUNTDOWN(米利斯,1000,意见);
            countDown.start();            //告诉AppWidgetManager对当前应用程序窗口小部件进行更新
            appWidgetManager.updateAppWidget(appWidgetId,意见);
        }
    }
}

但它不工作的方式。窗口小部件的TextView不刷新,甚至改变。这似乎是倒数不会启动。

也许我犯了一个错误的地方,但我是新的android开发,所以我想你能帮助我。


解决方案

 你必须设置文本部件后更新
使用
 appWidgetManager.updateAppWidget(appWidgetId,意见);
 后
  views.setTextViewText(R.id.countWidget,天);

I want to implement a CountDownTimer in a widget. What I came up with is:

import android.content.Intent;
import android.os.CountDownTimer;
import android.widget.RemoteViews;
import android.widget.TextView;

public class CountDown extends CountDownTimer {
    private RemoteViews views;

    public CountDown(long millisInFuture, int countDownInterval, RemoteViews views) {
        super(millisInFuture, countDownInterval);

        this.views = views;
    }

    @Override
    public void onFinish() {
    }

    @Override
    public void onTick(long millisUntilFinished) {
        int days = (int) (millisUntilFinished / (1000 * 60 * 60 * 24));

            views.setTextViewText(R.id.countWidget, days);
    }
}

And the AppWidgetProvider class

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.TextView;

public class CountDownWidgetProvider extends AppWidgetProvider {
    private Calendar currentDate = Calendar.getInstance();
    private Calendar firstDecember = Calendar.getInstance();

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Date date = new Date();

        this.firstDecember.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
        this.currentDate.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
        this.firstDecember.set(2012, 11, 1, 0, 0, 0); // TODO: Set to December
        this.currentDate.setTime(date);

        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch MainActivity
            Intent intent = new Intent(context, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.count_down_app_widget);
            views.setOnClickPendingIntent(R.id.countWidget, pendingIntent);

            long millis = this.firstDecember.getTimeInMillis() - this.currentDate.getTimeInMillis();

            CountDown countDown = new CountDown(millis, 1000, views);
            countDown.start();

            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

But it does not work that way. The widgets TextView does not refresh or even change. It seems like the CountDown does not start.

Maybe I made a mistake somewhere, but I'm new to android development, so I thought you could help me out.

解决方案

you have to update widget after setting the text
use 
 appWidgetManager.updateAppWidget(appWidgetId, views);


 after
  views.setTextViewText(R.id.countWidget, days);

这篇关于在AppWidgetProvider实现CountDownTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆