首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android - SMS发送意图未收到100%的时间

问Android - SMS发送意图未收到100%的时间
EN

Stack Overflow用户
提问于 2016-03-17 12:54:02
回答 1查看 356关注 0票数 2

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

代码语言:javascript
复制
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);

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

代码语言:javascript
复制
<receiver android:name="._broadcastReceivers.SMSSentBroadcastReceiver">
        <intent-filter android:priority="999">
            <action android:name="SMS_SENT_2" />
        </intent-filter>
</receiver>

我知道的一个事实是,对于一些消息,广播接收机没有得到的意图,但短信实际上是发送。

我如何可靠地确定短信是否“离开了电话”?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-18 18:01:07

与其使用FLAG_ONE_SHOT,不如确保PendingIntent是唯一的。最简单的方法是使用显式Intent而不是隐式Intent,并确保每个Intent中的操作是唯一的。下面是一个例子:

代码语言:javascript
复制
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);
}

通过将当前时间戳附加到每个操作中,我确保了操作是唯一的。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36061152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档