Why setOnClickPendingIntent two widgets gives the same widgetId?

There are two widgets
SkWidgetProvider
public class SkWidgetProvider extends AppWidgetProvider {

@Override
 public void onUpdate(Context Context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

 Log.i("SK", "Update");

 for (int wid: appWidgetIds) {
 Log.i("SK", "Update -" + wid);
}

 // Build the intent to call the service
 Intent intent = new Intent(context.getApplicationContext(),
SkWidgetService.class);
 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

 // Update the widgets via the service
context.startService(intent);
}
}


When you first start, the provider pays 9 and 10 and the method is invoked widgetId = widgetId 9 and = 10.
Update
I/SK: Update - 9
I/SK: Update - 10
I/SK: Service
I/SK: Service - 9
I/SK: Service - 10
I/SK: Run - 9
I/SK: Set new - 9
I/SK: Run - 10
I/SK: Set new - 10


But after installing setOnClickPendingIntent when you click on either of the two widgets is given only 10

I/SK: Update
I/SK: Update - 10
I/SK: Service
I/SK: Service - 10
I/SK: Run - 10
I/SK: Set new - 10


SkWidgetService
public class SkWidgetService extends Service {

 private Context call mcontext;
 private AppWidgetManager mAppWidgetManager;

@Override
 public int onStartCommand(Intent intent, int flags, int startId) {

 Log.i("SK", "Service");

 call mcontext = getApplicationContext();

 mAppWidgetManager = AppWidgetManager.getInstance(call mcontext);

 int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

 for (int widgetId : appWidgetIds) {

 Log.i("SK", "Service -" + widgetId);

update(widgetId);
}

 return super.onStartCommand(intent, flags, startId);
}

 private void update(final int widgetId) {

 RemoteViews remoteViews = new RemoteViews(call mcontext.getPackageName(),
R. layout.widget_layout);

remoteViews.setTextViewText("test");

 mAppWidgetManager.updateAppWidget(widgetId, remoteViews);

 new Handler().postDelayed(new Runnable() {
@Override
 public void run() {

 Log.i("SK", "Run -" + widgetId);

updateWidgetView(widgetId);
}
 }, 1000);
}

 private void updateWidgetView(int widgetId) {

 RemoteViews remoteViews = new RemoteViews(call mcontext.getPackageName(),
R. layout.widget_layout);

 remoteViews.setTextViewText("test run");

 // Register an onClickListener
 Intent clickIntent = new Intent(call mcontext, SkWidgetProvider.class);

 int[] widgetIds = {widgetId};

 for (int ids : widgetIds) {
 Log.i("SK", "Set new -" + ids);
}

clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
 clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);

 PendingIntent pendingIntent = PendingIntent.getBroadcast(
 call mcontext, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT
);

 remoteViews.setOnClickPendingIntent(R. id.update, pendingIntent);
 mAppWidgetManager.updateAppWidget(widgetId, remoteViews);

stopSelf();
}

@Nullable
@Override
 public IBinder onBind(Intent intent) {
 return null;
}
}


What I do not understand?
June 10th 19 at 15:50
1 answer
June 10th 19 at 15:52
Solution
Try to change int requestCode in the pendingIntent generation. For example, using Random. It must be unique, and not 0.
Thank you. I'll know ) - Clifton.Treut commented on June 10th 19 at 15:55
It needs to be unique within a widget? If you use widgetId it will be fine? - Clifton.Treut commented on June 10th 19 at 15:58
As I understand it, if you are one and the same pendingIntent you use within the widget - you can use widgetId. If within the widget there are 2 different pendingIntent, you need to have different requestCode. But I could be wrong. - Cedrick.Wiega commented on June 10th 19 at 16:01
Yes , I only have one click on the widget. Thanks again, the second day I suffer )) - Clifton.Treut commented on June 10th 19 at 16:04

Find more questions by tags Android