尝试识别SMS传递确认 [英] Trying to ID the SMS delivery confirmation
问题描述
我当前正在尝试获取每个发送的SMS的确认.我需要确保已发送我的SMS,所以我使用了BroadCastReceived
来获取信息:
I am currently trying to get the confirmation for each sended SMS. I need to be sure that my SMS are send, so I used a BroadCastReceived
to get the information :
Intent sentIntent = new Intent(SMS_SEND);
sentIntent.putExtra("key", idSms);
PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
SmsManager manager = SmsManager.getDefault();
try{
manager.sendTextMessage(exp, null, sms, sentPI, null);
put("sending " + sms); //Just a method to print in a textview use has a console
} catch (IllegalArgumentException e){
put("Exception " + e.getMessage());
}
并使用像这样的广播接收器
and use a broadcast receiver like this
public void onReceive(Context context, Intent intent){
String idsms = intent.getExtras().getString("key");
switch (getResultCode()) {
case Activity.RESULT_OK:
put("ACK : #" + idsms);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
case SmsManager.RESULT_ERROR_RADIO_OFF:
case SmsManager.RESULT_ERROR_NULL_PDU:
case SmsManager.RESULT_ERROR_NO_SERVICE:
put("BOOM " + getResultCode() + "\n\tfrom sms #" + idsms);
break;
}
}
直到我尝试同时发送多条消息之前,这项工作一直很吸引人,额外的接收总是来自最后一次短信发送,因此我无法确定发送哪些文本和不发送哪些文本.
This work like a charm until I try to send multiple messages at the same time, the extra receive is always from the last SMS send, so I can't ID which text are send and which are not.
这是将要发生的简单例子.
Here is a simple example of what will happen.
当我使用循环发送3sms时:
When I use a loop to send 3sms:
id:1,消息:SMS 1
id:2,消息:SMS 2
id:3,消息:SMS 3
id : 1, message : SMS 1
id : 2, message : SMS 2
id : 3, message : SMS 3
接收到的人将会得到:
ACK:#3
ACK:#3
确认:#3
ACK : #3
ACK : #3
ACK : #3
我知道这来自PendingIntent.FLAG_UPDATE_CURRENT,但找不到解决方案.任何人都可以向我解释如何使用PendingIntent.getBroadcast(..)来管理此问题,或者至少使我处于正确的轨道.
I understand that this come from the PendingIntent.FLAG_UPDATE_CURRENT but I can't find a solution. Anyone can explain to me how I should use the PendingIntent.getBroadcast(..) to be able to manage this or at least to put me on the right track.
推荐答案
您的问题是由于PendingIntent
可以被系统重用的事实,如果有关请求的某些内容没有不同的话.在您的代码中,您正在传递FLAG_UPDATE_CURRENT
,这将导致每次请求PendingIntent
时更新存储的Intent
及其附加功能.这就是为什么您要为每个消息获取id : 3
的原因.为了解决这个问题,您可以每次使用唯一的请求代码(第二个参数)调用getBroadcast()
,这将为每个请求创建一个新的PendingIntent
,每个请求都有一个单独的Intent
和自己的附加功能.
Your problem is due the fact that PendingIntent
s can be reused by the system, if certain things about the requests are not different. In your code, you're passing FLAG_UPDATE_CURRENT
, which is causing the stored Intent
and its extras to be updated each time a PendingIntent
is requested. This is why you're getting id : 3
for each of the messages. To correct this, you can call getBroadcast()
with a unique request code (the second parameter) each time, which will create a new PendingIntent
for each request, each with a separate Intent
with their own extras.
对于您而言,假设每个消息的idSms
都是唯一的,则修复应该很简单.
In your case, the fix should be simple, assuming that idSms
is unique for each message.
PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(),
Integer.parseInt(idSms),
sentIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
这篇关于尝试识别SMS传递确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!