How does Android send a text message to send a single message, only send the next message after the last successful send

Source: Internet
Author: User

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

1. Question: encountered in the project such as the following requirements: To send a text message has n, the implementation of a one-piece send and after the last text message sent successfully before sending the next one.

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

When you send a lot of information at the same time, it will produce a lot of the same pendingintent, and then the Android OS will update the pendingintent data to the latest, so the toast ID is the latest data and the data will be overwritten. This can be used to synchronize data.


If the text message is more than 70 characters, send it out in multiple text messages.
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) {
Infer whether 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??? , normally they should be 0,1,2.

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

//Assume that the text message is more than 70 characters to send this text message into multiple text messages

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) {
Infer whether 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));}





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.