【问题标题】:Android Widget Won't UpdateAndroid 小部件不会更新
【发布时间】:2012-09-13 13:51:20
【问题描述】:

我一直在为 Android 开发一个小部件。它应该做的一件事是显示一周中的当前日期和月份中的日期。我认为我的代码没问题,但由于某种原因它永远不会更新。我的提供程序中的更新周期设置为 30 分钟,但我认为这并不重要(无论如何我都尝试将其设置为 1 秒,但它没有改变任何东西)。另外,如果我让它在 LogCat 中打印一周中的当前日期和月份中的一天的值,它工作正常,那么我真的不知道它为什么不更新。请帮帮我!这是我的代码:

public class Henk extends AppWidgetProvider {

AppWidgetManager appWidgetManager;
ComponentName componentName;
RemoteViews remoteViews;
LocationManager locationManager;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    // Update the current date
    this.appWidgetManager = appWidgetManager;
    componentName = new ComponentName(context, Henk.class);
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);

    SimpleDateFormat dayofweekformat = new SimpleDateFormat("EEEE");
    Date dayofweekdate = new Date(System.currentTimeMillis());
    String dayofweeklowercase = dayofweekformat.format(dayofweekdate);
    String dayofweek = dayofweeklowercase.toUpperCase();

    SimpleDateFormat monthformat = new SimpleDateFormat("MMMM dd, yyyy");
    Date monthdate = new Date(System.currentTimeMillis());
    String month = monthformat.format(monthdate);

    Log.d("TAG", "----> DAY OF WEEK: " + dayofweek); // Fine in LogCat
    Log.d("TAG", "----> MONTH AND DATE: " + month); // Fine in LogCat

    remoteViews.setTextViewText(R.id.widget_textview1, dayofweek);
    remoteViews.setTextViewText(R.id.widget_textview2, month);

    appWidgetManager.updateAppWidget(componentName, remoteViews);

}
}

编辑:

我已经实现了Doomsknight提供的解决方案,所以现在我认为我的onUpdate()方法应该没问题。它仍然没有显示我的日期和日期。然而,我注意到,当我测试运行它时,onUpdate() 实际上是在我的配置活动关闭之前执行的。在我的配置活动中,我有以下代码来初始化我的小部件(至少它应该这样做),我认为错误就在这里:

public void onClick(View view) {
    // Launch the Widget and close the configuration Activity
    Intent intent2 = new Intent(context, Henk.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent2, 0);
    remoteViews.setOnClickPendingIntent(R.id.configuration_button, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetID, remoteViews);

    Log.d("TAG", "----> APP WIDGET ID: " + appWidgetID);
    Log.d("TAG", "----> REMOTEVIEWS: " + remoteViews);

    Intent result = new Intent();
    result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetID);
    setResult(RESULT_OK, result);
    finish();
}

【问题讨论】:

  • 仅供参考,小部件只能每 30 分钟更新一次,如果您选择一秒,则事件。

标签: android widget onupdate


【解决方案1】:

您必须记住,一个应用程序可以有多个小部件。 用户可以在您的屏幕上放置两个或更多。

您错过的是遍历 Ids。您传入了一些您生成的组件 ID,而不是小部件 ID,因此它不会更新正确的小部件。

小部件至少只能更新 30 分钟。除非您使用AlarmManager,否则每 1 秒将不起作用。你的情况不需要这个。

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    for (int appWidgetId : appWidgetIds) {

            remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
            SimpleDateFormat dayofweekformat = new SimpleDateFormat("EEEE");
            Date dayofweekdate = new Date(System.currentTimeMillis());
            String dayofweeklowercase = dayofweekformat.format(dayofweekdate);
            String dayofweek = dayofweeklowercase.toUpperCase();

            SimpleDateFormat monthformat = new SimpleDateFormat("MMMM dd, yyyy");
            Date monthdate = new Date(System.currentTimeMillis());
            String month = monthformat.format(monthdate);

            Log.d("TAG", "----> DAY OF WEEK: " + dayofweek); // Fine in LogCat
            Log.d("TAG", "----> MONTH AND DATE: " + month); // Fine in LogCat

            remoteViews.setTextViewText(R.id.widget_textview1, dayofweek);
            remoteViews.setTextViewText(R.id.widget_textview2, month);

        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }
}

【讨论】:

  • @Doomsknight 感谢您的回复!我现在明白了这个问题,这非常好。但是,我已经按照您在上面发布的代码中的方式实现了 for 循环,但它仍然没有更新。我可以在代码中使用 appWidgetId : appWidgetIds 对吗?
  • @Doomsknight 我认为问题在于小部件在配置活动关闭之前运行 onUpdate() 。我已经编辑了我的帖子以包含我的配置活动的代码。也许您可以在那里看到逻辑错误?
猜你喜欢
  • 2012-01-06
  • 2016-11-18
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-29
相关资源
最近更新 更多