您的位置:首页 > 移动开发 > Android开发

android 发送短信 怎样做到一条一条的发送,仅仅有在上一条发送成功之后才发送下一条短信

2014-07-29 11:38 483 查看
android发送短信截获上一条发送是否成功,然后再来发送下一条短信

1.问题:在项目中遇到例如以下要求:待发短信有N条,实现一条一条的发送并在上一条短信发送成功之后再来发送下一条。

for(int i=0;i<3;i++){

sendSMS(10086, text1, i);

}

private void sendSMS(String toAddress, String body, Long id) {

// ---sends an SMS message to another device---

SmsManager sms = SmsManager.getDefault();

String SENT_SMS_ACTION = "SENT_SMS_ACTION";

// create the sentIntent parameter

Intent sentIntent = new Intent(SENT_SMS_ACTION);

sentIntent.putExtra("id", id);

PendingIntent sentPI = PendingIntent.getBroadcast(

ListOutgoingActivity.this, 0, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT);

//你同一时候发送非常多信息的话,会产生非常多一样的PendingIntent,然后Android操作系统会把pendingIntent的数据更新到最新,所以toast的ID是最新的数据,曾经的数据会被覆盖掉。这个能够用来同步数据。

// 假设短信内容超过70个字符 将这条短信拆成多条短信发送出去

if (body.length() > 70) {

ArrayList<String> msgs = sms.divideMessage(body);

for (String msg : msgs) {

sms.sendTextMessage(toAddress, null, msg, sentPI,

null);

}

} else {

System.out.println("body====" + body);

sms.sendTextMessage(toAddress, null, body, sentPI, null);

}

BroadcastReceiver sendMessage = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

// 推断短信是否发送成功

switch (getResultCode()) {

case Activity.RESULT_OK:

Long id = intent.getLongExtra("id", -12);

//截取每次发送短信的ID,可是toast的id都是2???,正常情况下应该各自是0,1,2

Toast.makeText(ListOutgoingActivity.this,

id +"发送成功", Toast.LENGTH_SHORT).show();

break;

default:

Toast.makeText(ListOutgoingActivity.this,

"发送失败", Toast.LENGTH_LONG).show();

break;

}

}

};

registerReceiver(sendMessage, new IntentFilter(

SENT_SMS_ACTION));}

2.解决的方法:如今的解决方法是,收到上一条信息发送成功或者失败后,在发送下一条数据

int i=0;

sendSMS(10086,test, i) ;

private void sendSMS(String toAddress, String body, Long id) {

// ---sends an SMS message to another device---

SmsManager sms = SmsManager.getDefault();

String SENT_SMS_ACTION = "SENT_SMS_ACTION";

// create the sentIntent parameter

Intent sentIntent = new Intent(SENT_SMS_ACTION);

sentIntent.putExtra("id", id);

PendingIntent sentPI = PendingIntent.getBroadcast(

ListOutgoingActivity.this, 0, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT);

// 假设短信内容超过70个字符 将这条短信拆成多条短信发送出去

if (body.length() > 70) {

ArrayList<String> msgs = sms.divideMessage(body);

for (String msg : msgs) {

sms.sendTextMessage(toAddress, null, msg, sentPI,

null);

}

} else {

System.out.println("body====" + body);

sms.sendTextMessage(toAddress, null, body, sentPI, null);

}

BroadcastReceiver sendMessage = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

// 推断短信是否发送成功

switch (getResultCode()) {

case Activity.RESULT_OK:

Long id = intent.getLongExtra("id", -12);

Toast.makeText(ListOutgoingActivity.this,id +"发送成功", Toast.LENGTH_SHORT).show();

i++;

if(i<3){

sendSMS(10086,test,i)

}

break;

default:

Toast.makeText(ListOutgoingActivity.this,

"发送失败", Toast.LENGTH_LONG).show();

break;

}

}

};

registerReceiver(sendMessage, new IntentFilter(

SENT_SMS_ACTION));}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: