I'm trying to use one widget definition and one widget layout in my
application, but I would like to allow the user to install the 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