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