【问题标题】:How to pass extra data to a broadcast receiver with android remote input如何使用android远程输入将额外数据传递给广播接收器
【发布时间】:2018-07-17 21:21:51
【问题描述】:

我已经看到了这个问题的几个重复项,所以如果我错过了一个,我很抱歉,但我没有找到一个解决方案,例如 here 我正在尝试做与此相同。我想使用直接回复(消息传递样式)通知,但我需要添加一些额外的数据(recipientId 等),我很困惑我可以在哪里添加这些额外的数据,我的意图、待处理意图和 RemoteInput 的代码看起来像这个

    String replyLabel = getString(R.string.notif_action_reply);
    intent = new Intent(this, ReplyReceiver.class);

    replyPendingIntent = PendingIntent.getBroadcast(this,0,intent,0);
    RemoteInput remoteInput = new 
    RemoteInput.Builder(NOTIFICATION_REPLY).setLabel(replyLabel).build();
    NotificationCompat.Action action = new 
    NotificationCompat.Action.Builder(R.drawable.ic_send_white_24dp,
            this.getString(R.string.reply), replyPendingIntent)
            .addRemoteInput(remoteInput)
            .setAllowGeneratedReplies(true)
            .build();

现在要发送额外的数据,我尝试将其添加为字符串

intent.putExtra(Constants.MSG_RECIPIENT, message.getRecipientId());

作为捆绑包

Bundle bundle = new Bundle();
bundle.putString(Constants.MSG_RECIPIENT, message.getRecipientId());
intent.putExtras(bundle);

在意图和作为 RemoteInput 的一部分

RemoteInput remoteInput = new RemoteInput.Builder(NOTIFICATION_REPLY)
.setLabel(replyLabel).addExtras(bundle).build();

在尝试取回数据时,我尝试从 onReceive 意图中以字符串或捆绑包的形式获取数据

intent.getExtras(); 
intent.getStringExtra(Constants.MSG_RECIPIENT);

但似乎没有什么对我有用的结果总是在另一边为空(数据在发送时绝对不为空)有人可以告诉我如何添加意图数据并在另一边检索它吗?

更新

好的,现在可以了,我将为其他人添加工作代码,以便发送数据,我将其添加到第一个意图并在待处理的意图上调用 FLAG_UPDATE_CURRENT

    String replyLabel = getString(R.string.notif_action_reply);
    intent = new Intent(this, ReplyReceiver.class);
    //intent.putExtras(bundle);
    intent.putExtra(Constants.MSG_SENDER_NAME, message.getSenderName());
    intent.putExtra(Constants.MSG_SENDER, message.getSenderId());
    intent.putExtra(Constants.MSG_RECIPIENT_NAME, 
    message.getRecipientName());
    intent.putExtra(Constants.MSG_RECIPIENT, message.getRecipientId());

    replyPendingIntent = PendingIntent.getBroadcast
    (this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    RemoteInput remoteInput = new 
    RemoteInput.Builder(NOTIFICATION_REPLY).setLabel(replyLabel).build();
    NotificationCompat.Action action = new 
    NotificationCompat.Action.Builder(R.drawable.ic_send_white_24dp,
            this.getString(R.string.reply), replyPendingIntent)
            .addRemoteInput(remoteInput)
            .setAllowGeneratedReplies(true)
            .build();

为了接收它,我在 onReceive 中使用它

 String senderId = intent.getStringExtra(Constants.MSG_SENDER_NAME);

【问题讨论】:

  • 尝试将PendingIntent.FLAG_UPDATE_CURRENT 添加到getBroadcast(): replyPendingIntent = PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);此外,您没有显示将“附加”添加到Intent 的位置。您必须在致电 PendingIntent.getBroadcast() 之前添加它们。
  • 非常感谢这对我有用乐于接受它

标签: android android-intent bundle android-pendingintent remote-input


【解决方案1】:

尝试将PendingIntent.FLAG_UPDATE_CURRENT 添加到getBroadcast()

replyPendingIntent = 
        PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

此外,您没有显示将“附加”添加到 Intent 的位置。您必须在致电PendingIntent.getBroadcast() 之前添加它们。

【讨论】:

    猜你喜欢
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    相关资源
    最近更新 更多