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