安卓小部件无法更新

4
我一直在为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();
}

请注意,即使您选择每秒钟更新一次,widget 也只能每30分钟更新一次。 - Stephane Mathis
1个回答

2

请记住,一个应用程序可能有多个小部件。用户可以将两个或更多的小部件放在屏幕上。

您忽略了遍历ID的步骤。您传递了一些组件ID,而不是小部件ID,因此它无法更新正确的小部件。

小部件只能最少每30分钟更新一次。每秒钟更新一次是行不通的,除非您使用 AlarmManager。在您的情况下不需要这样做。

@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吗? - Zero
@Doomsknight 我认为问题在于小部件在配置活动关闭之前运行了 onUpdate()。我已经编辑了我的帖子,包括我的配置活动代码。也许你能看到那里的逻辑错误? - Zero

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接