Android detects whether the previous message is sent successfully, and then sends the Next message.
1. problem: the following requirements are met in the project: There are N messages to be sent, so that one message can be sent one by one and the next message can be sent after the previous message is sent successfully.
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 );
// If you send a lot of information at the same time, a lot of identical PendingIntent will be generated, and then the Android operating system will update the pendingIntent data to the latest, so the toast ID is the latest data, the previous data will be overwritten. This can be used to synchronize data.
// If the text message contains more than 70 characters, split the text message into multiple text messages and send them out.
If (body. length ()> 70 ){
ArrayList 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 ){
// Determine whether the text message is sent successfully
Switch (getResultCode ()){
Case Activity. RESULT_ OK:
Long id = intent. getLongExtra ("id",-12 );
// Intercept the ID of each sent message, but the toast id is 2 ???, Normally, the values are 0, 1, and 2.
Toast. makeText (ListOutgoingActivity. this,
Id + "sent successfully", Toast. LENGTH_SHORT). show ();
Break;
Default:
Toast. makeText (ListOutgoingActivity. this,
"Failed to send", Toast. LENGTH_LONG). show ();
Break;
}
}
};
RegisterReceiver (sendMessage, new IntentFilter (
SENT_SMS_ACTION ));}
2. solution: the solution is to send the next data after the previous message is successfully sent or fails.
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 );
// If the text message contains more than 70 characters, split the text message into multiple text messages and send them out.
If (body. length ()> 70 ){
ArrayList 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 ){
// Determine whether the text message is sent successfully
Switch (getResultCode ()){
Case Activity. RESULT_ OK:
Long id = intent. getLongExtra ("id",-12 );
Toast. makeText (ListOutgoingActivity. this, id + "sent successfully", Toast. LENGTH_SHORT). show ();
I ++;
If (I <3 ){
SendSMS (10086, test, I)
}
Break;
Default:
Toast. makeText (ListOutgoingActivity. this,
"Failed to send", Toast. LENGTH_LONG). show ();
Break;
}
}
};
RegisterReceiver (sendMessage, new IntentFilter (
SENT_SMS_ACTION ));}