0

我有两个小部件和一个配置活动。而且我在更新小部件时遇到了问题。AppWidgetProvider 类之一如下所示:

public class HolidaysWidgetProvider extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    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];           
        HolidaysBlackWidgetProvider.updateAppWidget(context, appWidgetId);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.holidays_appwidget);
        views.setTextViewText(R.id.yearTV, Integer.toString(y));
        views.setTextViewText(R.id.monthTV, Integer.toString(m));
        appWidgetManager.updateAppWidget(appWidgetId, views);
        super.onUpdate(context, appWidgetManager, appWidgetIds);

    }
}

而且我的小部件没有更新。但是在我添加以下代码后,我的小部件开始更新:

    Intent configIntent = new Intent(context, HolidaysWidgetProvider .class);
    configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    configIntent.setData(Uri.withAppendedPath(Uri.parse("abc" + "://widget/id/"), String.valueOf(appWidgetId)));
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, configIntent, 0);
    views.setOnClickPendingIntent(R.id.widget, pendingIntent);

为什么添加此代码会使我的小部件工作?

4

1 回答 1

2

根据 getBroadcast PendingIntent 中的代码,不添加 setData 是相等的。所以将pendingIntent设置为remoteView可能会放错appWidgetId。设置数据只是一个技巧

于 2013-09-08T18:02:28.137 回答