I created a widget that when clicked activates a PendingIntent. The problem is when I have more than one widget on the screen only the latest one will start the PendingIntent.
I have read some about a unique request code, but not figured this out.
Any ideas how I can have multiple widgets and the PendingIntents work for each?
Here is a snippet of my code:
Intent openApp = new Intent(context, RunningTally.class); openApp.putExtra("widgetId", appWidgetId); PendingIntent pendingAppIntent = PendingIntent.getActivity(context, 0, openApp, PendingIntent.FLAG_CANCEL_CURRENT ); views.setOnClickPendingIntent(R.id.openFull, pendingAppIntent);
A PendingIntent is a token that you give to a foreign application (e.g. NotificationManager, AlarmManager, Home Screen AppWidgetManager, or other 3rd party applications), which allows the foreign application to use your application's permissions to execute a predefined piece of code.
A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it.
PendingIntent is a wrapper of Intent . The foreign app that receives the PendingIntent , doesn't know the content of Intent which is wrapped by PendingIntent . The mission of foreign app is to send back the intent to owner when some conditions are met (For example: alarm with schedule, or notification with click...).
So happens that after posting my question, I came up with an answer. I pass in my appWidgetId as the "unique" request code and voila! Here is the snippet now:
Intent openApp = new Intent(context, RunningTally.class); openApp.putExtra("widgetId", appWidgetId); PendingIntent pendingAppIntent = PendingIntent.getActivity(context, appWidgetId, openApp, PendingIntent.FLAG_CANCEL_CURRENT); views.setOnClickPendingIntent(R.id.openFull, pendingAppIntent);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With