Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top