我每天发送超过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>我知道的一个事实是,对于一些消息,广播接收机没有得到的意图,但短信实际上是发送。
我如何可靠地确定短信是否“离开了电话”?
发布于 2016-03-18 18:01:07
与其使用FLAG_ONE_SHOT,不如确保PendingIntent是唯一的。最简单的方法是使用显式Intent而不是隐式Intent,并确保每个Intent中的操作是唯一的。下面是一个例子:
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>或明确声明它已导出,否则不会导出它。
https://stackoverflow.com/questions/36061152
复制相似问题