I realized that one more change was needed to your code five seconds after
sending my first response :)

You are using this:

void updateAppWidget(int[] appWidgetIds, RemoteViews views)

which takes an int[] array of widget ids, but only one RemoteViews. Thus,
all your widget instances receive the same data, the last remote views that
gets built.

What you need to do is:

1 - Use void updateAppWidget(int appWidgetId, RemoteViews views), whch
updates one widget

2 - Change the loop structure to go something like this:

   for(int j = 0;j < appWidgetIds.length;j++){
            RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layoutmain);
            Intent i = new Intent(context.getApplicationContext(),
BatteryProvider.class);
            i.setAction("MyCode");
            i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetIds[j]);
            i.setData(Uri.parse(i.toUri(Intent.URI_INTENT_SCHEME)));
            PendingIntent pi = PendingIntent.getBroadcast(context,j, i,0);
            views.setOnClickPendingIntent(R.id.imageViewLayer,pi);
            appWidgetManager.updateAppWidget(appWidgetIds[j], views);
      }

Now you'll be updating your widgets one by one, with each receiving its own
RemoteViews object, having its own pending intent.

-- K

2012/8/7 f.Audire <[email protected]>

> I actually used to have it inside the loop, cant remember why I moved it
> out. But I put it back in and still no difference.
>
> Am Montag, 6. August 2012 23:34:16 UTC+2 schrieb Kostya Vasilyev:
>
>> Move the call to appWidgetManager.**updateAppWidget inside the loop.
>>
>> -- K
>>
>> 2012/8/6 f.Audire <[email protected]>
>>
>>> Hi, I dont understand why my PendingIntents arent unique. The toast in
>>> OnReceive always shows the Id of the last created Widget .
>>>
>>> @Override
>>>     public void onUpdate(Context context, AppWidgetManager
>>> appWidgetManager,
>>>             int[] appWidgetIds){
>>>    super.onUpdate(context, appWidgetManager, appWidgetIds);
>>>
>>>    RemoteViews views = new RemoteViews(context.**getPackageName(),
>>> R.layout.widget_layoutmain);
>>>    for(int j = 0;j < appWidgetIds.length;j++){
>>>             Intent i = new Intent(context.**getApplicationContext(),
>>> BatteryProvider.class);
>>>             i.setAction("MyCode");
>>>             i.putExtra(AppWidgetManager.**EXTRA_APPWIDGET_ID,
>>> appWidgetIds[j]);
>>>             i.setData(Uri.parse(i.toUri(**Intent.URI_INTENT_SCHEME)));
>>>             PendingIntent pi = PendingIntent.getBroadcast(**context,j,
>>> i,0);
>>>             views.setOnClickPendingIntent(**R.id.imageViewLayer,pi);
>>>    }
>>>    appWidgetManager.**updateAppWidget(appWidgetIds, views);
>>> }
>>>
>>> @Override
>>>     public void onReceive(Context context, Intent intent) {
>>>
>>>    super.onReceive(context, intent);
>>>    Bundle extras = intent.getExtras();
>>>    Toast.makeText(context, String.valueOf(extras.getInt(**
>>> AppWidgetManager.EXTRA_**APPWIDGET_ID)), Toast.LENGTH_SHORT).show();
>>> }
>>>
>>> I have have no Idea why this doesnt work. I've seen so many examples
>>> that do almost exactly the same. I have tried different flags, different
>>> data, different actions and different requestCodes but still I seem to have
>>> only one PendingIntent.
>>>
>>> What am I doing wrong?
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Android Developers" group.
>>> To post to this group, send email to android-developers@**
>>> googlegroups.com <[email protected]>
>>> To unsubscribe from this group, send email to
>>> android-developers+**[email protected]<android-developers%[email protected]>
>>> For more options, visit this group at
>>> http://groups.google.com/**group/android-developers?hl=en<http://groups.google.com/group/android-developers?hl=en>
>>
>>
>>  --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to