这或多或少是这个问题的延续,但更为复杂: How to monitor each of Sent SMS status?
我正在尝试创建一个转储文件,以说明我的应用发送的短信成功发送或传递的时间。我尝试按照上面的示例进行操作,甚至为每个发送/传递都指定了自己的唯一ID(又名DELIVER1,DELIVER2等),但由于接收者从未捡到任何东西,最终我一无所获。请注意,与大多数短信应用不同,我的邮件被设计为快速连续发送短信。
这是我的代码的片段:
//Outside a for loop
String sentI = "SMS_SENT";
String delvI = "SMS_DELIVERED";
int z=0;
//inside a for loop
try
{
PendingIntent sentIntent = PendingIntent.getBroadcast(getApplicationContext(),0,new Intent(sentI.concat(Integer.toString(z))),0);
PendingIntent deliveredIntent = PendingIntent.getBroadcast(getApplicationContext(),0,new Intent(delvI.concat(Integer.toString(z))),0);
//Detecting When a Message Has been Successfully Sent
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(getResultCode()==Activity.RESULT_OK)
{
sentDump.add(System.currentTimeMillis());
}
else
{
sndfailDump.add((Long.toString(System.currentTimeMillis())).concat(",").concat("'").concat("Sms Failed to Send.").concat("'\n"));
}
}
}, new IntentFilter(sentI.concat(Integer.toString(z))));
//Detecting When a Message has been successfully delivered
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(getResultCode()==Activity.RESULT_OK)
{
delvDump.add(System.currentTimeMillis());
}
else
{
delvFailDump.add((Long.toString(System.currentTimeMillis())).concat(",").concat("'").concat("Sms failed to deliver.").concat("'\n"));
}
}
}, new IntentFilter(delvI.concat(Integer.toString(z))));
if(apiState==true)
{
sayHello(message[z]);
}
else
{
SmsManager.getDefault().sendTextMessage(phoneNum, null, message[z], sentIntent, deliveredIntent);
}
smsLog.add(System.currentTimeMillis());
msgNoTracker = msgNoTracker + 1;
} catch (Exception e){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
//Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
//crashTrack.add((Long.toString(System.currentTimeMillis())).concat("\n").concat(e.toString())); //original
crashTrack.add((Long.toString(System.currentTimeMillis())).concat(",").concat("'").concat(sw.toString()).concat("'\n"));
//reset all incremental value back one for proper repeat
j=j-1;
continue; //jump back to for loop
}
try{
Thread.sleep(messageDelay);
} catch(InterruptedException e){
e.printStackTrace();
}
z+=1;
转储的目的仅仅是记录发送和传送时间,而不是管理短信是否已成功发送...