I have two widgets and one configuration activity for them. And I had problem with updating widgets. One of AppWidgetProvider classes looked like:

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);

    }
}

And my widget didn't update. But after I added the following code my widget began to update:

    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);

Why adding this code make my widget works?

有帮助吗?

解决方案

According to code in getBroadcast PendingIntent without adding setData are equals. So setting pendingIntent to remoteView can possible put wrong appWidgetId. Setting data is just a hack

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top