10.2 SmsManager (SMS Manager)
Category Android Basic Tutorial
Introduction to This Section:
>
This section introduces the SmsManager in Android, which, as the name suggests, is used to manage mobile phone text messages. The application scenarios for this class are not numerous; it is typically used when we send text messages, of course, these are text-only messages. As for MMS, it is too complex, and in the era of various social apps like QQ and WeChat, would you send an MMS that costs one yuan per message? So in this section, we will only discuss sending ordinary text messages! Official documentation: SmsManager
1. Invoking the System's Text Message Sending Feature:
>
This involves sending the prepared recipient and message to the system's text message sending interface, where the user verifies the correctness of the recipient and content before clicking send! In other words, it is about invoking the system's text messaging window, and there are certain advantages to this approach:
Sending messages this way, the app can omit one permission for sending text messages during installation, so security apps like 360 will not alert users during installation: "This APP has text message permissions and may secretly send text messages," which users strongly dislike. Of course, some people will install directly without looking, while others may think it's a disgusting app that secretly sends text messages, and they will not install it, or they may directly prohibit our APP from sending text messages, which may lead to some exceptions or even app crashes when our APP is sending text messages! So if your app needs to send text messages for verification or payment, it is recommended to use this method!
Core Code :
public void SendSMSTo(String phoneNumber, String message) {
// Check if the entered phoneNumber is a valid phone number
if (PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)) {
// Uri.parse("smsto") is the conversion to a specific Uri, a fixed way of writing
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + phoneNumber));
intent.putExtra("sms_body", message);
startActivity(intent);
}
}
2. Invoking the System-Provided SMS Interface to Send Text Messages
>
This requires permission to send text messages
uses-permission android:name="android.permission.SEND_SMS" />
We directly call the SmsManager's provided SMS interface to send text messages:
sendTextMessage (destinationAddress, scAddress, text, sentIntent, deliverIntent);
The parameters are as follows:
destinationAddress : The recipient's phone number
scAddress : The number of the SMS center, if null, the current default SMS service center is used
text : The content of the text message
sentIntent : Information about the text message sending status: (Intent of sending status) If not null, when the message is successfully sent or fails, this PendingIntent is broadcast. The result code is Activity.RESULT_OK indicating success, or one of RESULT_ERROR_GENERIC_FAILURE, RESULT_ERROR_RADIO_OFF, RESULT_ERROR_NULL_PDU indicating an error. For RESULT_ERROR_GENERIC_FAILURE, sentIntent may include an additional "error code" containing a value specific to a radio broadcast technology, usually only useful when troubleshooting. Each SMS-based application controls the detection of sentIntent. If sentIntent is null, the caller will detect all unknown applications, which will result in a smaller number of SMS being sent during detection.
deliverIntent : Information on whether the text message has been received by the recipient: (Intent of reception status) If not null, when this text message is sent to the recipient, this PendingIntent will be broadcast, and the status report generated by the pdu (referring to the data unit transmitted between peer layers) will be expanded to the data ("pdu")
Just the phone number, message center, text content, whether the message was sent successfully, and whether the recipient has received the message!
Core Code :
```java
public void sendSMS(String phoneNumber, String message) {
// Get the SMS manager
android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();
// Split the text message content (due to the length limit of mobile text messages), it seems that the length limit is 140 characters, that is,
// only 70 Chinese characters can be sent, more than that needs to be split into multiple text messages
// The fourth and fifth parameters, if there is no need to monitor the sending status and reception status, you can write null
List<String> divideContents
2.5.4 AutoCompleteTextView (Auto Complete Text Box) Basic Usage
2.5.8 Notification (Status Bar Notification) Detailed Explanation
[3.5 Listening to EditText
8.3.4 Paint API - Xfermode and PorterDuff Detailed Explanation (I)
8.3.5 Paint API - Xfermode and PorterDuff Detailed Explanation (II)
8.3.6 Paint API - Xfermode and PorterDuff Detailed Explanation (III)
8.3.7 Paint API - Xfermode and PorterDuff Detailed Explanation (IV)
8.3.8 Paint API - Xfermode and PorterDuff Detailed Explanation (V)
8.3.14 Paint Several Enum/Constant Values and ShadowLayer Shadow Effect
8.3.17 Detailed Explanation of Canvas API (Part 2) - Collection of Clipping Methods
8.3.18 Detailed Explanation of Canvas API (Part 3) - Matrix and drawBitmapMesh
[8.4.2 Android Animation Collection - Tween Animation](android