SmsManager (SMS Manager)
Introduction to this section:
This section introduces SmsManager (Short Message Manager) in Android. As the name implies, it is used to manage mobile phone text messages. There are not many application scenarios of this type. Generally, we only use this API when sending text messages. Of course, this kind of text message is Text messages are too complicated for MMS, and in the era when QQ, WeChat and various social apps are rampant, you will send one for 1 yuan. MMS? So in this section we only discuss sending ordinary text messages! Official document: SmsManager
1. Call the system to send text messages:
is to send the written recipient and content to The system's interface for sending text messages requires the user to verify whether the recipient's content is indeed correct before clicking send! To put it bluntly, it is to call the system's text messaging window. This has certain benefits:
Send text messages in this way. When the app is installed, you can write one less permission to send text messages, then such as 360 When installing this kind of security software The user will not be reminded: "This APP has SMS permissions and may secretly send text messages." And users are very concerned about secretly sending text messages. It's disgusting. Of course, some people install it without looking at it, and some people may think it can secretly send text messages. It's such a disgusting application. I won't pretend. Or directly prohibit our APP from sending text messages, then some abnormalities may occur when our APP sends text messages, or The application crashes directly and so on! So if your application 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){ //判断输入的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 SMS interface provided by the system to send text messages
This requires the permission to send text messages
##uses-permission android:name="android.permission.SEND_SMS" />
We directly call the SMS interface provided by SmsManager to send text messages:sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliverIntent);
The parameters are:
- destinationAddress: The phone number of the recipient
- scAddress: The number of the SMS center, if null, use the current default SMS service center
- text: SMS content
- sentIntent: SMS sending status information: (Sending status Intent) If not null, this PendingIntent is broadcast when the message is successfully sent or fails. The result code is Activity.RESULT_OK Indicates success, or RESULT_ERROR_GENERIC_FAILURE, RESULT_ERROR_RADIO_OFF, RESULT_ERROR_NULL_PDU One indicates an error. Corresponding to RESULT_ERROR_GENERIC_FAILURE, sentIntent may include an additional "error code" containing an A radio technology-specific value that is usually only useful when fixing malfunctions. Every SMS-based application controls detection of sentIntent. If sentIntent is empty, the caller will detect all unknown applications, which will result in a smaller number of SMS being sent when detected.
- deliverIntent: Status information of whether the text message has been received by the other party: (Intent of receiving status) If not null, when this message is sent to the recipient, the PendtingIntent will be broadcast. The pdu generated by the status report (referring to the data unit transferred between peer levels) will be expanded to the data ("pdu")
...So complicated, what is pdu egg? Okay, don’t worry, just know that these parameters are:
phone number, information center, text message content, monitoring whether the message is sent successfully, and monitoring whether the recipient accepts it!
Core Code
public void sendSMS(String phoneNumber,String message){ //获取短信管理器 android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault(); //拆分短信内容(手机短信长度限制),貌似长度限制为140个字符,就是 //只能发送70个汉字,多了要拆分成多条短信发送 //第四五个参数,如果没有需要监听发送状态与接收状态的话可以写null List divideContents = smsManager.divideMessage(message); for (String text : divideContents) { smsManager.sendTextMessage(phoneNumber, null, text, sentPI, deliverPI); } }
Maybe you also need to monitor whether the text message is sent successfully or whether the recipient receives the message, so just add the following:
1) Process the sentIntent
//处理返回的发送状态 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));
that returns the sending status 2) Process the deliverIntent that returns the receiving status:
//处理返回的接收状态 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 the knowledge of broadcasting. If you don’t know much about broadcasting, you can take a look:
Basic Introduction to Android Tutorial - BroadcastReceiver Test
Android Basic introductory tutorial - 4.3.2 BroadcastReceiver Pao Ding Jie Niu
Summary of this section:
Okay, this section introduces SmsManager to send text There are two ways to send text messages~very simple~it is recommended to use The first option, at least the user experience is better...