【问题标题】:how to pass a code to Broadcast receiver while receiver sms delevery report in android如何在android中接收短信发送报告时将代码传递给广播接收器
【发布时间】:2012-10-22 03:33:00
【问题描述】:

我想开发一个短信应用程序,它可以一次发送多条短信。我想为 SMS 设置一个 ID。在 SMS SENT 报告中,我想获取此 ID。所以,我会知道已经发送了一条特定的消息。

我已经添加了广播接收器。我也收到了 SMS SENT 报告。

例如:我发送了 10 条短信,收到了 8 条短信发送报告。如何判断哪 2 条消息没有发送,以便重新发送?

【问题讨论】:

  • 你的想法似乎不错。您是否尝试过添加 ID?

标签: android sms


【解决方案1】:

对于您发送的每条 SMS(或部分 SMS),您都需要提供一个 PendingIntent。在该 PendingIntent 中,您放置了一个 Intent,当 SMS(或部分)成功发送或未发送时,您将收到该 Intent。在这个 Intent 中,您可以使用 extras 放置额外的信息。因此,例如,在发送消息时,代码可能如下所示...

String receiverCodeForThisMessage = "STRING_CODE_FOR_MY_SMS_OUTCOME_RECEIVER";
int uniqueCodeForThisPartOfThisSMS = 100*numberSMSsentSoFar+PartNumberOfThisSMSpart;

Intent intent = new Intent(receiverCodeForThisMessage);
intent.putExtra("TagIdentifyIngPieceOfInformationOne", piece1);
intent.putExtra("TagIdentifyIngPieceOfInformationTwo", piece2);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueCodeForThisPartOfThisSMS, intent, 0);

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("phonenumber", "Fred Bloggs", "Hello!", pendingIntent, null);

但在此之前,您将注册一个接收器,该接收器将获得您设置的结果和意图;从那个意图中你提取信息片段:

registerReceiver(new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {
        // Get information about this message
        int piece1 = intent.getIntExtra("TagIdentifyIngPieceOfInformationOne", -1);
        int piece2 = intent.getIntExtra("TagIdentifyIngPieceOfInformationTwo", -1);

        if (getResultCode() == Activity.RESULT_OK) {
            // success code
        }
        else {
            // failure code
        }
}, new IntentFilter(receiverCodeForThisMessage));

【讨论】:

  • 谢谢你 Neil,我在 SENT REPORT 中得到了 id,但我有 3 个问题。 (1) 当我在 onReceive() 方法中检查意图时,它为所有消息提供相同的 id。 (2)当我通过给定的键获得 id 时,它返回-1。 (3)onReceiver被调用两次
  • 以相反的顺序... (3) - 如果您发送两条 SMS 消息或两部分消息,我希望 onReceiver 被调用两次。 (2) 你有没有检查过这些键是正确的.. 很容易打错。 (1) 这可能是因为它实际上只产生了一个待处理的意图(长篇故事),但如果创建待处理意图行中的第一个“0”是您发送的每个短信(部分)唯一的整数,您可能会发现它会更好.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 1970-01-01
  • 2022-11-28
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
相关资源
最近更新 更多