How does Android send text messages to send a single message, only send the next message after the previous one has been successfully sent

Source: Internet
Author: User

Android sends SMS to intercept the previous send is successful, then send the next SMS

1. Question: in the project encountered the following requirements: To send a text message has n, to achieve a single send and send the last message after the success of the next message sent.

  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---
        &NBSP ;            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);

You send a lot of information at the same time, there will be a lot of the same pendingintent, 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 is more than 70 characters, split the text message into multiple text messages and send it out.
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) {
Determine if the text message was sent successfully
Switch (Getresultcode ()) {
Case ACTIVITY.RESULT_OK:
Long id = intent.getlongextra ("id",-12);

//intercepts the ID of each SMS sent, but the toast ID is 2??? , 0,1,2 should normally be the case, respectively.

Toast.maketext (Listoutgoingactivity.this,
ID + "Send Success", Toast.length_short). Show ();

Break ;
Default:
Toast.maketext (Listoutgoingactivity.this,
"Send Failed", Toast.length_long). Show ();
Break ;
}
}
};
Registerreceiver (SendMessage, New Intentfilter (
sent_sms_action));}




2. Workaround: the solution now is to send the next piece of data after the last message was sent successfully or failed

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 is more than 70 characters, split the text message into multiple text messages and send it out .

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) {
Determine if the text message was sent successfully
Switch (Getresultcode ()) {
Case ACTIVITY.RESULT_OK:
Long id = intent.getlongextra ("id",-12);
Toast.maketext (Listoutgoingactivity.this,ID + "Send Success", Toast.length_short). Show ();
i++;

if (i<3) {

Sendsms (10086,test,i)

                                            }
Break ;
Default:
Toast.maketext (Listoutgoingactivity.this,
"Send Failed", Toast.length_long). Show ();
Break ;
}
}
};
Registerreceiver (SendMessage, New Intentfilter (
sent_sms_action));}





How does Android send text messages to send a single message, only send the next message after the previous one has been successfully sent

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.