PendingIntent在SmsManager中

huangapple go评论66阅读模式
英文:

PendingIntent in SmsManager

问题

在我制作的消息程序中,我使用了 pending intent 来接收消息发送和传递报告。当我发送多条消息并等待传递报告时,程序无法识别接收到的 pending intent 属于哪条消息。

我的代码如下:

sentSMS = PendingIntent.getBroadcast(this, msgId(), new Intent(SMS_SENT), 0);
deliverSMS = PendingIntent.getBroadcast(this, msgId(), new Intent(SMS_DELIVERED), 0);

根据我搜索到的信息,我必须使用请求代码参数,为每条消息分配一个请求代码 (来自数据库的每条消息的ID),但我不知道如何在 on receive 中接收它。

另外,我尝试了以下方法:

Intent deliveryIntent = new Intent();
deliveryIntent.putExtra("id", msgId());
deliverSMS = PendingIntent.getBroadcast(this, msgId(), deliveryIntent, 0);

Intent sendIntent = new Intent();
sendIntent.putExtra("id", msgId());
sentSMS = PendingIntent.getBroadcast(this, msgId(), sendIntent, 0);

但我没有收到任何额外的数据。

谢谢 🙏

英文:

In the messaging program I made, I used pending intent to receive the message sending and delivery report. When I send several messages and wait for the delivery report, the program does not recognize which message the received pending intent belongs to.
My code is:
sentSMS = PendingIntent.getBroadcast(this, msgId(), new Intent(SMS_SENT), 0);
deliverSMS = PendingIntent.getBroadcast(this, msgId(), new Intent(SMS_DELIVERED), 0);

According to what I searched for, I have to use the request code parameter for this, for each message I have assigned a request code **(the ID of each message from the database) **in pending intent, but I don't know how to receive it in on receive.

thanks

Also i tried this:

Intent deliveryIntent = new Intent();
          deliveryIntent.putExtra("id", msgId());
          deliverSMS = PendingIntent.getBroadcast(this, msgId(), deliveryIntent, 0);
          //************
          Intent sendIntent = new Intent();
         sendIntent.putExtra("id", msgId ());
          sentSMS = PendingIntent.getBroadcast(this, msgId(), sendIntent, 0);
         

But I did not receive anything in extra

TThanks 🙏

答案1

得分: 0

当创建 PendingIntent 时,需要在 getBroadcast() 调用的最后一个参数中指定 PendingIntent.UPDATE_CURRENT。这将确保每个 PendingIntent 都具有正确的 "id"。

onReceive() 中,您应该获取广播 Intent,它应包含名为 "id" 的 "extra" 与消息 ID。

但是,如果创建 Intent 时没有指定操作(ACTION)以及没有组件或包名称,您的 onReceive() 将永远不会被调用。请编辑您的问题并展示您如何设置您的 BroadcastReceiver

英文:

When you create the PendingIntent, you need to specify PendingIntent.UPDATE_CURRENT as the last parameter of the getBroadcast() call. This will ensure that each PendingIntent has the correct "id".

In onReceive() you should get the broadcast Intent and it should contain the "extra" named "id" with the message ID.

However, if you create the Intent without an ACTION and without a component or package name, your onReceive() will never get called. Please edit your question and show how you have set up your BroadcastReceiver.

huangapple
  • 本文由 发表于 2023年2月13日 23:20:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定