How does android send text messages one by one? The next text message is sent only after the previous message is sent successfully.

Source: Internet
Author: User

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 ));}





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.