5

I'm creating a widget with two buttons. One of them updates the content of the widget and the second one must launch an activity.

I have two PendingIntent for each action, but I can't make them both work. If one works the other one doesn't.

I've revised the code and can't understand what's wrong.

Any help will be very appreciated.

This is the code.

    RemoteViews controls = new RemoteViews(context.getPackageName(), R.layout.miwidget);

    Intent intent = new Intent("actony.com.ACTUALIZAR_WIDGET");
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);


    Intent intentSettings = new Intent();  
    intentSettings.setClass(context,WidgetConfig.class);  


    PendingIntent pendingIntentUpdate = PendingIntent.getBroadcast(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    controls.setOnClickPendingIntent(R.id.BtnActualizar, pendingIntentUpdate);

    PendingIntent pendingIntentSettings =  PendingIntent.getActivity(context, 0, intentSettings, 0);
    controls.setOnClickPendingIntent(R.id.botonSettings, pendingIntentSettings);
user986689
  • 111
  • 10

3 Answers3

2

Check this link to know which button has been clicked when there is two or more button in a widget..

https://stackoverflow.com/a/10733049/1331593

It should work... IF it does not work please let me know what is the problem...

Community
  • 1
  • 1
Tamal Samui
  • 738
  • 10
  • 14
2

Try adding the getActivity PendingIntent.FLAG_UPDATE_CURRENT aswell...

 PendingIntent pendingIntentSettings =  
      PendingIntent.getActivity(context, 0, intentSettings, PendingIntent.FLAG_UPDATE_CURRENT);

and if multiple widget's are possible add the widgetId there too.

Make sure both of the activities/broadcasts are listed in the manifest file.

Moreover, try creating the Intent with this constructor:

 Intent intent = new Intent(context,ACTUALIZAR_WIDGET.class);
 Intent intentSettings = new Intent(context,WidgetConfig.class);

add imports if needed.

Hope some of that will make you widget work.

GalDude33
  • 7,071
  • 1
  • 28
  • 38
0

You can try this code:

Intent read = new Intent(ctx, NotificationClick.class);
read.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
read.putExtra(Intent.EXTRA_SUBJECT, "READ");
PendingIntent readInt = PendingIntent.getActivity(ctx, 1, read, PendingIntent.FLAG_IMMUTABLE);

Intent reply = new Intent(ctx, NotificationClick.class);
reply.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
reply.putExtra(Intent.EXTRA_SUBJECT, "REPLY");
PendingIntent replyInt = PendingIntent.getActivity(ctx, 2, reply, PendingIntent.FLAG_IMMUTABLE);

NotificationManagerCompat nMgr = NotificationManagerCompat.from(ctx);
Notification newMessageNotification = new NotificationCompat.Builder(ctx, "MESSAGE_CHANNEL")
  .setSmallIcon(R.drawable.user_account)
  .setContentTitle(contact)
  .setContentText(text)
  .addAction(R.drawable.drafts, "Read", readInt)
  .addAction(R.drawable.drafts, "Reply", replyInt)
  .setGroup(MESSAGE_GROUP)
  .build();
nMgr.notify(100, newMessageNotification);
Prashant
  • 394
  • 1
  • 6
  • 18