两个带有PendingIntent的按钮 - Widget

5
我正在创建一个带有两个按钮的小部件。 其中一个按钮更新小部件的内容,而第二个按钮必须启动一个活动。
我为每个操作都有两个PendingIntent,但我无法使它们同时工作。如果一个可用,则另一个将无法使用。
我已经检查了代码,但不明白问题出在哪里。
非常感谢您的任何帮助。
这是代码。
    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);
3个回答

2

尝试添加getActivity PendingIntent.FLAG_UPDATE_CURRENT...


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

如果有多个小部件可用,请在那里添加widgetId。

确保活动/广播都在清单文件中列出。

此外,尝试使用此构造函数创建Intent:

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

如有需要,请添加导入项。

希望这些内容能够让您的小部件正常工作。


2

0
你可以尝试这段代码:
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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接