【问题标题】:Android - SMS sent intent not received 100% of timeAndroid - 100% 的时间未收到 SMS 发送的意图
【发布时间】:2016-03-17 12:54:02
【问题描述】:

我每天发送超过 400 条短信,但只有大约 95% 的短信我收到了发送到广播接收器的短信。消息每隔 5 秒发送一次。

ArrayList<String> partsArray = SmsManager.getDefault().divideMessage(this.message);
ArrayList<PendingIntent> sentPendingIntents = new ArrayList<>(partsArray.size());
ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<>(partsArray.size());
      .....

for (int i = 0; i < partsArray.size(); i++) {
        sentIntent = new Intent("SMS_SENT_2");
        deliveredIntent = new Intent(C.SMS_DELIVERED);
        sentPI = PendingIntent.getBroadcast(context, 0,sentIntent, PendingIntent.FLAG_ONE_SHOT);
        deliveredPI = PendingIntent.getBroadcast(context, 0, deliveredIntent, PendingIntent.FLAG_ONE_SHOT);

        sentPendingIntents.add(sentPI);
        deliveredPendingIntents.add(deliveredPI);
}

SmsManager sms = SmsManager.getDefault();
sms.sendMultipartTextMessage(phoneNumber, null,partsArray, sentPendingIntents, deliveredPendingIntents);

以及清单中的广播接收器:

<receiver android:name="._broadcastReceivers.SMSSentBroadcastReceiver">
        <intent-filter android:priority="999">
            <action android:name="SMS_SENT_2" />
        </intent-filter>
</receiver>

我知道,对于某些消息,广播接收者没有得到意图,但实际上发送了短信。

如何可靠地确定短信是否“离开手机”?

【问题讨论】:

    标签: android android-intent broadcastreceiver sms android-pendingintent


    【解决方案1】:

    您应该确保PendingIntents 是唯一的,而不是使用FLAG_ONE_SHOT。最简单的方法是使用显式 Intent 而不是隐式 Intent 并确保每个 Intent 中的 ACTION 是唯一的。这是一个例子:

    for (int i = 0; i < partsArray.size(); i++) {
        sentIntent = new Intent(this, _broadcastReceivers.SMSSentBroadcastReceiver.class);
        sentIntent.setAction("SMS_SENT_2" + System.currentTimeMillis());
        deliveredIntent = new Intent(this, DeliveryReceiver.class);
        deliveredIntent.setAction(C.SMS_DELIVERED + System.currentTimeMillis());
        sentPI = PendingIntent.getBroadcast(context, 0, sentIntent, 0);
        deliveredPI = PendingIntent.getBroadcast(context, 0, deliveredIntent, 0);
    
        sentPendingIntents.add(sentPI);
        deliveredPendingIntents.add(deliveredPI);
    }
    

    我通过将当前时间戳附加到每个 ACTION 来确保 ACTION 是唯一的。

    您现在可以从 BroadcastReceiver 的清单定义中删除 &lt;intent-filter&gt;,因为您不再需要它(您正在使用显式 Intent)。但是,请确保将 android:exported="true" 添加到 BroadcastReceiver 的清单定义中,因为除非您有 &lt;intent-filter&gt; 或明确声明它已导出,否则它不会被导出。

    【讨论】:

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