Introduction to this section:

This section brings the SmsManager (SMS Manager) in Android. As its name suggests, it is used to manage SMS on phones. However, this class does not have many application scenarios. Generally, this API is only used when sending text messages. Of course, this kind of SMS is plain text; MMS is too complex, and in an era where QQ, WeChat, and various social apps are rampant, would you send an MMS that costs 1 yuan each? So in this section, we only discuss sending ordinary text messages! Official documentation:SmsManager


1. Call the system's SMS sending function:

It means sending the prepared recipient and content to the system's SMS sending interface, and the user verifies whether the recipient and content are really correct before clicking send! In plain words, it calls the system's SMS window. There are certain benefits to doing this:

By sending SMS in this way, when the app is installed, it canomit one permission for sending SMSThen security software such as 360 will not remind users during installation: "This APP has SMS permission, it may secretly send text messages." Users are very disgusted by behavior that secretly sends SMS. Of course, some people install directly without reading, while others may think, "It might secretly send messages, what a disgusting app, I won't install it," or they might directly forbid our APP to send SMS. Then when our APP tries to send SMS, some exceptions may occur, or the app may crash! So if your app needs to send SMS for verification or payment, it is recommended to use this method!

Core code:

public void SendSMSTo(String phoneNumber,String message){    
    //判断输入的phoneNumber是否为合法电话号码  
    if(PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)){  
        //Uri.parse("smsto") 这里是转换为指定Uri,固定写法  
        Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+phoneNumber));        
        intent.putExtra("sms_body", message);              
        startActivity(intent);    
    }    
}    

2. Call the system-provided SMS interface to send SMS

This requires the SMS sending permission.

uses-permission android:name="android.permission.SEND_SMS"/>

We directly call the SMS interface provided by SmsManager to send SMS:

sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliverIntent);

The parameters are, in order:

  • destinationAddress: the phone number of the recipient
  • scAddress: the number of the SMS center; if null, the current default SMS service center is used
  • text: the SMS content
  • sentIntent: information about the SMS sending status: (the Intent of the sending status) If it is not null, when the message is successfully sent or fails, the PendingIntent will be 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. Corresponding to RESULT_ERROR_GENERIC_FAILURE, the sentIntent may include an additional "error code" containing a value specific to radio broadcasting technology, usually only useful when fixing faults. Every SMS-based application controls the detection of the sentIntent. If sentIntent is null, the caller will detect all unknown applications, which will result in a smaller number of SMS messages being sent during detection.
  • deliverIntent: status information on whether the SMS was received by the other party: (the Intent of the receiving status) If it is not null, when the SMS reaches the recipient, this PendingIntent will be broadcast. The pdu (referring to the data unit passed between peer layers) generated by the status report will be extended to the data ("pdu")

...So complex, what the heck is pdu? Well, don't worry about it. Simply know these parameters are:

phone number, SMS center, SMS content, listener for whether sending succeeded, and listener for whether the recipient received it—that's all!

Core code:

public void sendSMS(String phoneNumber,String message){  
    //获取短信管理器   
    android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();  
    //拆分短信内容(手机短信长度限制),貌似长度限制为140个字符,就是
    //只能发送70个汉字,多了要拆分成多条短信发送
    //第四五个参数,如果没有需要监听发送状态与接收状态的话可以写null    
    List<String> divideContents = smsManager.divideMessage(message);   
    for (String text : divideContents) {    
        smsManager.sendTextMessage(phoneNumber, null, text, sentPI, deliverPI);    
    }  
} 

You may also need to listen for whether the SMS was sent successfully or whether the recipient received the message, so just add the following:

1) Handling the returned sending statussentIntent

//处理返回的发送状态   
String SENT_SMS_ACTION = "SENT_SMS_ACTION";  
Intent sentIntent = new Intent(SENT_SMS_ACTION);  
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent,  0);  
//注册发送信息的广播接收者
context.registerReceiver(new BroadcastReceiver() {  
    @Override  
    public void onReceive(Context _context, Intent _intent) {  
        switch (getResultCode()) {  
        case Activity.RESULT_OK:
            Toast.makeText(context, "短信发送成功", 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:          //没有提供pdu
            break;      
        case SmsManager.RESULT_ERROR_NO_SERVICE:         //服务当前不可用
            break;              
        }  
    }  
}, new IntentFilter(SENT_SMS_ACTION)); 

2) Handling the returned receiving statusdeliverIntent:

//处理返回的接收状态   
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";  
//创建接收返回的接收状态的Intent  
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,"收信人已经成功接收", Toast.LENGTH_SHORT).show();  
   }  
}, new IntentFilter(DELIVERED_SMS_ACTION)); 

In addition, this involves broadcast knowledge. If you don't know much about broadcasts, you can take a look at:

Android Beginner Tutorial—BroadcastReceiver: First Try

Android Beginner Tutorial—4.3.2 BroadcastReceiver: In-depth Analysis


Section summary:

Alright, this section introduced two ways to send text SMS using SmsManager~ Very simple~ It is recommended to use the first method, at least for a better user experience...