There are two ways to call the interface in android to send SMS messages
- 2020-05-09 19:18:04
- OfStack
1. Call system SMS interface to send SMS directly; The main code is as follows:
// Call the SMS interface directly to send SMS messages
SmsManager smsManager = SmsManager.getDefault();
List<String> divideContents = smsManager.divideMessage(content);
for (String text : divideContents) {
smsManager.sendTextMessage("150xxxxxxxx", null, text, sentPI, deliverPI);
}
2. Adjust the system to send SMS; The main code is as follows:
Uri uri = Uri.parse("smsto:10010");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "102");
activity.startActivity(it);
Here we will focus on the first method, most of which comes from the Internet:
Get the SMS manager
SmsManager smsManager = SmsManager.getDefault();
Split message content (SMS length limit)
List<String> divideContents = smsManager.divideMessage(content);
Send the split content
List<String> divideContents = smsManager.divideMessage(content);
for (String text : divideContents) {
smsManager.sendTextMessage("150xxxxxxxx", null, text, sentPI, deliverPI);
}
Handle the send state returned
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent,
0);
// register the Broadcast Receivers
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(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 received state returned
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
// create the deilverIntent parameter
Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0,
deliverIntent, 0);
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
Toast.makeText(context,
" The recipient has received it successfully ", Toast.LENGTH_SHORT)
.show();
}
}, new IntentFilter(DELIVERED_SMS_ACTION));
Send SMS parameter description
smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
-- destinationAddress: target phone number
-- scAddress: SMS center number. Test can be left blank
-- text: text message content
-- sentIntent: send -- > China mobile -- > China mobile transmission failed > Return send success or failure signal -- > The subsequent processing is that this intent wraps the information about the status of the message
-- deliveryIntent: send -- > China mobile -- > China mobile successfully sent -- > Returns whether the other party received this message -- > The subsequent processing is: this intent wraps the status information of whether the message has been received by the other party (the supplier has sent it successfully, but the other party has not received it).