Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RemoteViews and setOnClickPendingIntent [duplicate]

I have a WidgetProvider and an Configure Activity

When the Widget is started it starts with the configure activity and I set it up by making a custom call to the widgetprovider

(which you will notice is from the sdk tutorial examples)

 // Push widget update to surface with newly set prefix
              AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
              AwarenessWidget.updateAppWidget(context, appWidgetManager,
                      mAppWidgetId, position);

            // Make sure we pass back the original appWidgetId
            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            setResult(RESULT_OK, resultValue);
            finish();

I pass the Widget ID to the function.... inside the widget I create a Intent like this:

  Intent configIntent = new Intent(context, Configure.class);
    configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

    PendingIntent pendingIntent = PendingIntent.getActivity
    (context, 0, configIntent,
    PendingIntent.FLAG_UPDATE_CURRENT);

    views.setOnClickPendingIntent(R.id.MainImage,pendingIntent);

    views.setImageViewResource(R.id.MainImage, lv_images[version]);

    appWidgetManager.updateAppWidget(appWidgetId, views);

I am always referencing the widget ID and even add it as a extra on the intent but when I get two of these widgets on the home screen the widget ID is always referencing the last placed widget ID

like image 612
GregM Avatar asked Nov 25 '22 17:11

GregM


1 Answers

I had a similar problem. Just add this to your config activity, where you set your PendingIntent:

Uri data = Uri.withAppendedPath(
    Uri.parse(URI_SCHEME + "://widget/id/")
    ,String.valueOf(appWidgetId));
intent.setData(data);

The variable URI_SCHEME is a String, and can be whatever you'd like.. ie - "ABCD" This causes each widget to have a unique PendingIntent.

like image 130
Snailer Avatar answered Dec 15 '22 10:12

Snailer