標籤:android style color 資料 io for art 問題
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));}