First, call the system SMS interface to send text messages directly.The main code is as follows:
Copy codeThe Code is as follows: // directly call the SMS interface to send a text message
SmsManager smsManager = SmsManager. getDefault ();
List <String> divideContents = smsManager. divideMessage (content );
For (String text: divideContents ){
SmsManager. sendTextMessage ("150 xxxxxxxx", null, text, sentPI, deliverPI );
}
Second: Call the system's text message functionThe main code is as follows:Copy codeThe 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 );
The first method is described here. Most of the information comes from the Internet:
Get SMS Manager
Copy codeThe Code is as follows: SmsManager smsManager = SmsManager. getDefault ();
Split text message content (SMS length limit)Copy codeThe Code is as follows: List <String> divideContents = smsManager. divideMessage (content );
Sent split contentCopy codeThe Code is as follows: List <String> divideContents = smsManager. divideMessage (content );
For (String text: divideContents ){
SmsManager. sendTextMessage ("150 xxxxxxxx", null, text, sentPI, deliverPI );
}
Processing returned sending statusCopy codeThe Code is as follows: 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 ));
Processing the returned receiving statusCopy 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 = PendingIntent. getBroadcast (context, 0,
DeliverIntent, 0 );
Context. registerReceiver (new BroadcastReceiver (){
@ Override
Public void onReceive (Context _ context, Intent _ intent ){
Toast. makeText (context,
"The recipient has successfully received the message", Toast. LENGTH_SHORT)
. Show ();
}
}, New IntentFilter (DELIVERED_SMS_ACTION ));
Description of parameters for sending SMS messages Copy codeThe Code is as follows: smsManager. sendTextMessage (destinationAddress, scAddress, text, sentIntent, deliveryIntent)
-- DestinationAddress: the destination phone number.
-- ScAddress: the number of the SMS center. This parameter can be left empty for testing.
-- Text: text message content
-- SentIntent: Send --> China Mobile failed to send --> return the sending success or failure signal --> after processing, this intent encapsulates the message sending status.
-- DeliveryIntent: Send --> China Mobile sends the message successfully --> Returns whether the recipient receives the message --> subsequent processing: this intent encapsulates the status information of the message received by the other party (the supplier has successfully sent the message but the other party has not received it ).