I'm making a widget in android which produces a random number when clicked. When the widget is alone on the home screen it works perfectly, however when you add multiple of them they start to generate random numbers at the same time. Whats happening is when an individual widget is clicked it updates all of them; resulting in many random numbers. What i want is each widget to be isolated from the others; basically when a widget is clicked it only updates itself and non of the others around it. I think this is achievable by getting the ID of the current widget and update that one only, as apposed to the update method updating all the widgets; how do i do this?
My code
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
// Create some random data
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Log.w("WidgetExample", String.valueOf(number));
// Set the text
remoteViews.setTextViewText(R.id.update, String.valueOf(number));
// Register an onClickListener
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
Summary:
This code is the update method and is called when a widget is clicked. I want this method to only update the widget ID that called it, not for this method to update all the widgets ID's on the home screen.
I had the same problem and I found this way to solve it:
1.To be able to distinguish between multiple instances of the same AppWidgetProvider, when registering the “onClick” event (intent) you must add an extra value with the widget ID (appWidgetId):
Intent clickIntent = new Intent(context, DigiStation.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, clickIntent, 0);
2.Update only the views of the current instance:
appWidgetManager.updateAppWidget(appWidgetId, views);
3.Android reuses intents, so when you create an intent, make sure you put an unique ID, or else the same intent used before will be triggered for all instances:
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, clickIntent, 0);
4.When handling the click event, get the appWidgetId from the “extras” payload of the intent.
You can found more useful details here.
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