Android development-Explanation of SmsManager (SMS manager) and androidsmsmanager
SmsManager is another very common service provided by Android. SmsManager provides a series of sendXxxMessage () methods for sending text messages.
SmsManager: Manages SMS operations, such as sending data, text messages, and PDU messages. Call the static method SmsManager. getDefault () to obtain this object.
|
Public Methods |
|
|
ArrayList <String> |
DivideMessage (String text)
When the length of a text message exceeds the maximum length of the SMS message, the text message is divided into several parts. |
|
|
|
|
|
|
|
|
|
|
|
Static SmsManager |
GetDefault ()
Obtain the default SmsManager instance. |
|
|
|
|
|
|
|
|
|
|
|
Void |
SendDataMessage (String destinationAddress, String scAddress, short destinationPort, byte [] data, PendingIntent sentIntent, PendingIntent deliveryIntent)
Sends SMS-based data to the specified application port. |
|
|
|
|
|
|
|
|
|
|
|
Void |
SendMultipartTextMessage (String destinationAddress, String scAddress, ArrayList <String> parts, ArrayList <PendingIntent> sentIntents, ArrayList <PendingIntent> deliveryIntents)
Send a multi-part text message based on SMS.
DivideMessage(String text) Splits messages into correct sizes. |
|
|
|
|
|
|
|
|
|
|
|
Void |
SendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
Send an SMS-based text. |
|
|
|
|
|
|
|
|
|
|
Note:
· ArrayList <String>DivideMessage(String text)
When the length of a text message exceeds the maximum length of the SMS message, the text message is divided into several parts.
Parameters:Text-- The initial message cannot be blank.
Return Value: OrderedArrayList <String>, Can be combined into an initial message
· Static SmsManagerGetDefault()
Obtain the default SmsManager instance.
Return Value:SmsManagerDefault instance
· VoidSendDataMessage(String destinationAddress,String scAddress,Short destinationPort,Byte [] data,PendingIntent sentIntent, PendingIntent deliveryIntent) sends an SMS-based data to the specified application port.
Parameters:
1 ),DestinationAddress-- Target address of the message
2 ),ScAddress-- The service center address or is null. Use the current default SMSC 3)DestinationPort-- Target Port Number of the message
4 ),Data-- The message body, that is, the data to be sent by the message
5 ),SentIntent-- If it is not null, The PendingIntent is broadcast when the message is successfully sent or fails. The result code is Activity. RESULT_ OK, indicating success, or RESULT_ERROR_GENERIC_FAILURE, RESULT_ERROR_RADIO_OFF, RESULT_ERROR_NULL_PDU, indicating an error. Corresponding to RESULT_ERROR_GENERIC_FAILURE,SentIntentIt may include an additional "error code" that contains a specific value of radio broadcast technology, which is generally only useful for fault fixing.
Every SMS-based application control DetectionSentIntent. IfSentIntentYes, the caller will detect all unknown applications, which will cause a small number of SMS messages to be sent during the detection.
6 ),DeliveryIntent-- If it is not null, the message is broadcast when it is successfully delivered to the recipient's PendingIntent.
Exception: IfDestinationAddressOrDataIf it is null, an IllegalArgumentException exception is thrown.
· VoidSendMultipartTextMessage(String destinationAddress,String scAddress,ArrayList <String> Parts,ArrayList <PendingIntent> sentIntents, ArrayList <PendingIntent> DeliverIntents)
Send a multi-part text message based on SMS.DivideMessage(String text) Splits messages into correct sizes.
Parameters:
1 ),DestinationAddress-- Target address of the message
2 ),ScAddress-- The service center address or is empty. Use the current default SMSC.
3 ),Parts-- OrderedArrayList <String>, Can be combined into an initial message
4 ),SentIntents-- FollowSendDataMessageThe method is the same, but here is a set of PendingIntent
5 ),DeliverIntents-- FollowSendDataMessageThe method is the same, but here is a set of PendingIntent
Exception: IfDestinationAddressOrDataIf it is null, an IllegalArgumentException exception is thrown.
· VoidSendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
Send an SMS-based text. The parameter has the same meaning as the existing one before the exception.
For SmsManager application examples, refer to: sending text messages in Android development.
In Android development, how can I call text message functions in detail ??
First, you should file AndroidManifest in the program list. permission for text messaging in xml 1 Figure 1 shows the brief text messaging interface, which consists of two TextView components, two EditText components, and one Button component, add the event private EditText txt_num; private EditText txt_content; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); txt_num = (EditText) this.findViewById(R.id.txt _ num); txt_content = (EditText) this.findViewById(R.id.txt _ content ); Button btn_send = (Button) this. findViewById (R. id. btn_send); btn_send.setOnClickListener (new OnClickListener () {public void onClick (View v) {String str_num = txt_num.getText (). toString (); // obtain the telephone number String str_content = txt_content.getText (). toString (); // get the SMS content SmsManager manager_sms = SmsManager. getDefault (); // get the text message manager. // split the text message into ArrayList texts = smsManager because the text message may be long. divideMessage (str_content); ( String text: texts) {smsManager. sendTextMessage (str_num, null, text, null, null); // send each message} Toast. makeText (SMSActivity. this, "sent successfully! ", Toast. LENGTH_LONG). show (); // prompt success});} Now, the short message sending function has been introduced.
How can I determine the status of multiple messages sent by android ?? <Activity android: name = "SmsActivity" android: label =
SmsManager smsManager = SmsManager. getDefault (); // gets the SMS manager, which is the default SMS manager of the Android system and can be used to send text messages.
List <String> contents = smsManager. divideMessage (content); // multiple SMS messages. This section splits long text messages into multiple messages for sending.
For (String text: contents ){
SmsManager. sendTextMessage (destinationAddress, scAddress, text, sentIntent, deliveryIntent); // send SMS
}
There are two results when you send the SMS. One is whether the SMS is successfully sent, the other is whether the recipient receives the SMS successfully, and the other is not determined by the Android program, SMS messages are sent by SMS base stations, such as mobile phones. We only need to send the SMS to the mobile wireless communication network. If the SMS is successfully sent over the network, mobile will return a signal, which will be captured by the program.
The program uses an Asynchronous Method to capture signals. Because it is impossible to keep the thread on waiting for the returned signal, the last two parameters of sendTextMessage (), sentIntent and deliveryIntent, are used to receive and send status signals.
SentIntent indicates whether the SMS is successfully sent; deliveryIntent indicates whether the receiver has received the SMS Intent. If you do not want to receive the returned message, you can set it to null.
After the Intent is passed in, if the mobile network returns a message indicating that the text message has been sent successfully or failed, the Operating System Broadcasts the Intent asynchronously, and you will know the status of the text message.