代码之家  ›  专栏  ›  技术社区  ›  martinseal1987

如何通过Android远程输入将额外数据传送到广播接收器

  •  1
  • martinseal1987  · 技术社区  · 7 年前

    我看过这个问题的两个副本,所以如果我错过了一个,我会提前道歉,但没有一个我发现有解决办法,以此类推。 here 不过,我也在做同样的事情。我想使用直接回复(消息风格)通知,但我需要添加一些额外的数据(recipientid等),我对在哪里可以添加这些额外的数据感到困惑我的意图、待定意图和远程输入代码如下所示

        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 = new RemoteInput.Builder(NOTIFICATION_REPLY)
    .setLabel(replyLabel).addExtras(bundle).build();
    

    在试图取回数据的过程中,我试图从onReceive意图中获取数据,作为字符串或捆绑包。

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

    但对我来说似乎什么都不起作用,结果总是在另一端为空(发送时数据绝对不为空),有人能告诉我如何添加意图数据并在另一端检索它吗?

    更新

    好了,现在可以工作了,我要为其他人添加工作代码,以便发送数据,我要将其添加到第一个意图中,并调用标记“更新”当前待处理的意图。

        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);
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   David Wasser    7 年前

    尝试添加 PendingIntent.FLAG_UPDATE_CURRENT getBroadcast() :

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

    此外,您没有显示在何处添加了“额外”的意图。打电话前必须添加 PendingIntent.getBroadcast() .