Easy Tutorial
❮ Python Redis Intro Android Tutorial Fragment Demo5 ❯

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:

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

❮ Python Redis Intro Android Tutorial Fragment Demo5 ❯