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));}
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));}
相关文章推荐
- android群发短信时判断短信是否发送成功
- Android后台发送短信 是否发送成功提醒
- Android--判断发送短信后的状态/发送成功Or发送失败
- Android 向系统发送一条短信
- android sendTextMessage每发送一条短信都弹权限提示
- 如何判断android 短信发送(sendTextMessage)是否成功
- 如何判断android 短信发送(sendTextMessage)是否成功
- android判断短信是否发送成功[动态注册广播]
- android发送短信带监听是否发送成功功能
- Android--判断发送短信后的状态/发送成功Or发送失败
- Android--判断发送短信后的状态/发送成功Or发送失败
- Android发送短信,并监听短信发送后是否发送成功的实现方法
- Android--判断发送短信后的状态/发送成功Or发送失败
- android 短信是否发送成功
- Android中怎样做到自己定义的广播仅仅能有指定的app接收
- Android监听SMS发送状态并获取短信服务中心号码
- android 中发送短信
- Android自制发送短信程序
- Android中发送短信和彩信
- Android发送接收短信的代码示例