You are actually using the same PendingIntent for both widgets.
http://developer.android.com/reference/android/app/PendingIntent.html
>>>
If the creating application later re-retrieves the same kind of
PendingIntent (same operation, same Intent action, data, categories, and
components, and same flags), it will receive a PendingIntent
representing the same token
<<<
When checking for duplicate (existing) PendingIntent objects, intent
extras are not considered.
To create two distinct PI objects, you can either:
- Use distinct request codes when calling PI.getActivity;
- Use distinct data URIs in the Intents used to create the PIs.
Either way, that'll give you two distinct PI objects, and each can go to
its own widget.
-- Kostya
29.06.2011 21:47, Tony ?????:
I'm trying to use one widget definition and one widget layout in my
application, but I would like to allow the user to installthe very
same widget many times on the screen.
Then, if the user clicks the corresponding widget layout on the
screen, I would like to launch an activity and to show which widget
instance (widget ID) has launched the activity.
For example, if the user installed the same widget twice, both widget
instances will have the same layout (on different places on the
screen), and two different IDs (like ID #56 and ID# 57).Then, if the
user clicks the first widget layout on the screen, he should see a
simple activity screen "Called from widget id #56". If the user clicks
the second widget layout, he should see "Called from widget # 57"
I am setting the onClickListner, using
remoteViews.setOnClickPendingIntent and I pass the widgetID as an
extra in the Intent.
It works fine, if the user install the widget only once. However, if
he installs the same widget more than once, it doesn’t send the proper
Pending Intent.
Seems like every time when a new PendingIntent is scheduled with
remoteViews.setOnClickPendingIntent for the same widget class (even
from different widget instances), it will override the previous
scheduled PendingIntent, or it will be ignored (depending of the
pending intent flag).
So when the user clicks the different widget layouts on the screen,
the OS will launch only the very first or the very last Pending Intent
for all of them, and the activity will receive only the very first ,or
the very last widget ID, no matter which layout is pressed.
In our example, the user will see always "called from widget # 56" ,
no matter which layout is pressed.
For different widget instances, I would like to schedule different
PendingIntents with remoteViews.setOnClickPendingIntent, but I am not
sure if this is possible.
I tried unsuccessfully all available flags
:PendingIntent.FLAG_NO_CREATE,PendingIntent.FLAG_ONE_SHOT,
PendingIntent.FLAG_CANCEL_CURRENT, PendingIntent.FLAG_UPDATE_CURRENT,
Intent.FILL_IN_ACTION, Intent.FILL_IN_CATEGORIES
,Intent.FILL_IN_DATA,Intent.FILL_IN_COMPONENT, Intent.FILL_IN_PACKAGE
, Intent.FILL_IN_SOURCE_BOUNDS
Here is a short snippet in the body of the widget (AppWidgetProvider):
(Please note, that I am displaying the widgetID on the widget layout
at the same time when I set remoteViews.setOnClickPendingIntent with
the same widget id. The widgetID is displayed correctly, the problem
is coming only when I try to consume the onClick event - the new
scheduled PendingIntent is either ignored, or it overrides the
previous PendingIntent)
...
public void onUpdate( Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
widgetId = appWidgetIds[0];
prepareOnClickListener(context);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
private void prepareOnClickListener(Context context){
if (remoteViews==null) {
remoteViews = new RemoteViews(context.getPackageName(), widgetLayout);
}
Intent startIntent = new Intent(context, TestActivity.class);
startIntent.setAction(START_ACTIVITY);
startIntent.putExtra(WIDGET_ID, widgetId);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
startIntent, Intent.FILL_IN_DATA);
// I tried different flags here, with no success
remoteViews.setOnClickPendingIntent(R.id.intent_layout, pendingIntent);
remoteViews.setTextViewText(R.id.widget_id,"This widget ID is "+id);
}
--
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
--
Kostya Vasilyev
--
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