在AppWidgetProvider中不是onUpdate [英] not onUpdate in AppWidgetProvider

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

问题描述

我的Android小部件未更新日期.

My Android widget is not updating the date.

这是一个只有日期的小部件,没有其他内容.

It's a widget with only date, no more.

package com.android.widget;

import java.util.Timer;
import java.util.TimerTask;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;

public class MyCalendarWidget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1,
                1000);
    }

    private class MyTime extends TimerTask {
        RemoteViews remoteViews;
        AppWidgetManager appWidgetManager;
        ComponentName thisWidget;
        Fecha fec = new Fecha();

        public MyTime(Context context, AppWidgetManager appWidgetManager) {
            this.appWidgetManager = appWidgetManager;
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
            thisWidget = new ComponentName(context, MyCalendarWidget.class);
        }

        @Override
        public void run() {
            remoteViews.setTextViewText(R.id.widget_textview, fec.DiaActual(false)+"\r\n"
                                                             +fec.DiaActual(true) +" de "
                                                             +fec.MesActual(false)+" de "
                                                             +fec.AñoActualNumerico());
            appWidgetManager.updateAppWidget(thisWidget, remoteViews);
        }
    }
}

推荐答案

请先阅读文档, AppWidget指南 AppWidgetProvider .这说明:

Please begin by reading the documentation, AppWidget Guide and AppWidgetProvider. That explains:

  • 您的onUpdate()方法需要通过updateAppWidget()将RemoteViews发送到窗口小部件.
  • 每秒唤醒一次会耗尽电池电量.您当然不希望在手机处于休眠状态时更新小部件.
  • 每次调用remoteViews.setTextViewText()都会将更多数据附加到RemoteViews对象.这样会在内存中累积过多的数据,并使小部件变慢.而是每次都获取一个新的RemoteViews实例.

我关于您的更新未发生的假设是,操作系统在提供程序完成对Intent的响应后会停止您的小部件提供程序的组件.您可以通过Log.d()测试该假设.

My hypothesis about your updates not happening is the OS stopping your widget provider's component when the provider finishes responding to an Intent. You can test that hypothesis via Log.d().

您可以注册 ACTION_TIME_TICK 广播,该广播告诉您时间改变时,每分钟一次.请进行测试,以确保手机在休眠状态下不会运行(例如保持通话计数),并随时在此处报告.

You can register for an ACTION_TIME_TICK broadcast that tells you when the time changes, once per minute. Do test that it doesn't run when the phone sleeps (e.g. keep a count of calls) and feel free to report back here.

[添加] 实际上,显示日期(每天更新一次)要比日期+时间(每分钟更新一次)容易得多;您的问题代码试图对其进行更新每秒一次),而无需消耗大量电池电量即可做到这一点.

[Added] It's much easier in practice to display the date (and update it once per day) than the date + time (and update it once per minute; your question code tried to update it once per second), and to do this without using much battery power.

  • 设置updatePeriodMillis="0".
  • 在AndroidManifest.xml中的窗口小部件提供程序的<intent-filter>中添加<action android:name="android.intent.action.DATE_CHANGED"/>.
  • 在AppWidgetProvider中,覆盖

  • Set updatePeriodMillis="0".
  • Add <action android:name="android.intent.action.DATE_CHANGED"/> to the <intent-filter> for the widget provider in AndroidManifest.xml.
  • In the AppWidgetProvider, override

public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if (Intent.ACTION_DATE_CHANGED.equals(intent.getAction())) { updateAllMyWidgets(context); }
}

  • 使用DateUtils.formatDateTime()设置日期格式.

  • Use DateUtils.formatDateTime() to format the date.

    是的,该错误 code.google.com/p/android/如果时钟向后调整,就会发生问题/细节?id = 2880 .

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

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