First: Call the system SMS interface to send SMS directly;The main code is as follows:
Copy the codeThe code is as follows:
//Directly call the SMS interface to send text messages
SmsManager smsManager = ();
List<String> divideContents = (content);
for (String text : divideContents) {
("150xxxxxxxx", null, text, sentPI, deliverPI);
}
Second: activate the system's text message function;The main code is as follows:
Copy the codeThe code is as follows:
Uri uri = ("smsto:10010");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
("sms_body", "102");
(it);
Here we mainly explain the first method, most of which comes from the Internet:
Get SMS Manager
Copy the codeThe code is as follows:
SmsManager smsManager = ();
Split SMS content (mobile SMS length limit)
Copy the codeThe code is as follows:
List<String> divideContents = (content);
Send the split content
Copy the codeThe code is as follows:
List<String> divideContents = (content);
for (String text : divideContents) {
("150xxxxxxxx", null, text, sentPI, deliverPI);
}
Process the returned send status
Copy the codeThe code is as follows:
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sentPI = (context, 0, sentIntent,
0);
// register the Broadcast Receivers
(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
(context,
"SMS sent successfully", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
}
}
}, new IntentFilter(SENT_SMS_ACTION));
Process the returned receive status
Copy the codeThe code is as follows:
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
// create the deilverIntent parameter
Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI = (context, 0,
deliverIntent, 0);
(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
(context,
"The recipient has successfully received it", Toast.LENGTH_SHORT)
.show();
}
}, new IntentFilter(DELIVERED_SMS_ACTION));
Parameter description for sending text messages
Copy the codeThe code is as follows:
(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
-- destinationAddress: target phone number
-- scAddress: SMS center number, test can be filled without filling
-- text: SMS content
-- sentIntent: Send --> China Mobile --> China Mobile's send failed --> Return to the sending successful or failed signal --> Subsequent processing That is, this intention wraps the information about the sending status of the SMS
-- deliveryIntent: Send --> China Mobile --> China Mobile sent successfully --> Return to whether the other party received this message --> Follow-up processing That is: this intention packages the status information of whether the text message was received by the other party (the supplier has sent it successfully, but the other party has not received it).