Update current Android widget id in update method

I am making a widget in android that calls a random number when pressed. When the widget is one on the main screen, it works fine, however, when you add several of them, they start to generate random numbers at the same time. What happens when an individual widget is clicked, it updates all of them; which leads to many random numbers. I want each widget to be isolated from the others; basically, when a widget is clicked, it updates only itself, and not other people around it. I think that this can be achieved by obtaining the identifier of the current widget and updating only the one that was added to the update method that updates all widgets; how to do it?

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 an update method and is called when the widget is clicked. I want this method to update the widget ID that called it, and not this method to update all widget IDs on the main screen.

+7
source share
1 answer

I had the same problem and found a way to solve it:
1. To be able to distinguish several instances of the same AppWidgetProvider, when registering an event (intention) onClick, you must add an additional value with the widget identifier (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 representations of the current instance:

 appWidgetManager.updateAppWidget(appWidgetId, views); 

3.Android reuses intentions, so when you create an intent, make sure you put a unique identifier, or the same intention that was used earlier will be triggered for all instances:

 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, clickIntent, 0); 

4. When processing the click event, get appWidgetId from the "additional functions" payload. You can find more useful information here .

+9
source

All Articles