android 傳送簡訊 怎樣做到一條一條的發送,僅僅有在上一條發送成功之後才發送下一條簡訊

來源:互聯網
上載者:User

標籤: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));}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.