在Widget AppWidgetProvider中使用TimerTask和Timer [英] Using TimerTask and Timer with Widget AppWidgetProvider

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

问题描述

我正在使用Android创建一个时钟小部件。我已经测试了时钟,逻辑似乎没有问题,但我需要让它每分钟更新一次。

我在xml中将updatePerodMillis设置为60000,但安卓将其限制为30分钟,因此它每半小时才更新一次。

我做了一些研究,除了我正在调用的方法需要通过onUpdate()方法传递给它的AppWidgetManager和AppWidgetId之外,Service或AlarmManager可能可以工作。

我尝试了两种(类似的)方法来使分钟更新生效:

static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager,
                            final int appWidgetId) {

    final Handler mHandler = new Handler();
    TimerTask minuteTask = new TimerTask() {

        @Override
        public void run() {
            mHandler.post(new Runnable() {
                @Override
                public void run() {

                    //Do Stuff
            });
        }
    };

    timer = new Timer();

    // schedule the task to run starting now and then every minute
    timer.scheduleAtFixedRate(minuteTask, 0l, 1000 * 60);

}

和:

static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager,
                            final int appWidgetId) {

    TimerTask minuteTask = new TimerTask() {

        @Override
        public void run() {

            //Do stuff
        }
    };

    timer = new Timer();

    // schedule the task to run starting now and then every minute
    timer.scheduleAtFixedRate(minuteTask, 0l, 1000 * 60);

}

非常感谢您的帮助。

谢谢。

编辑:在我调试和发布这篇文章的过程中,我最终让应用程序安装了半个小时。首次调用onUpdate()时,计时器似乎已在半小时后启动,但在最初的30分钟内仍不起作用。

推荐答案

好的,终于开始工作了(因为有考试,所以两个帖子之间间隔很长)。

我最终使用了服务、待定意图和警报管理器来满足我的需求。

ClockWidget.class:

private static final String TAG = ClockWidget.class.getSimpleName();

@Override
public void onReceive(@NonNull Context context, @NonNull Intent intent) {
    super.onReceive(context, intent);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    ComponentName componentName = new ComponentName(context, ClockWidget.class);
    onUpdate(context, appWidgetManager, appWidgetManager.getAppWidgetIds(componentName));
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.d(TAG, "onUpdate");

    ComponentName componentName = new ComponentName(context, ClockWidget.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(componentName);

    Intent intent = new Intent(context.getApplicationContext(), UpdateService.class);
    intent.setAction(UpdateService.ACTION_UPDATE);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
    context.startService(intent);

    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    m.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60, pendingIntent);
}

UpdateService.class:

private static final String TAG = UpdateService.class.getSimpleName();

public static final String ACTION_UPDATE = "uk.co.thebillington.laxe.ACTION_UPDATE";

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent == null) {
        return super.onStartCommand(null, flags, startId);
    }

    String action = intent.getAction();
    if (action == null) {
        return super.onStartCommand(intent, flags, startId);
    }

    Log.d(TAG, String.format("onStartCommand: %s", action));

    if (action.equals(ACTION_UPDATE)) {
        int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
        for (int widgetId : allWidgetIds) {
            updateWidget(widgetId);
        }
    }

    return super.onStartCommand(intent, flags, startId);
}

private void updateWidget(int widgetId) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());

    RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.clock);

    //Do stuff

    appWidgetManager.updateAppWidget(widgetId, views);
}

这篇关于在Widget AppWidgetProvider中使用TimerTask和Timer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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