Android can call the interface to send text messages in two different ways _android

Source: Internet
Author: User

First: Call the system SMS interface to send SMS directly ; The main code is as follows:

Copy Code code as follows:

Directly call SMS interface to send text messages
Smsmanager Smsmanager = Smsmanager.getdefault ();
list<string> dividecontents = smsmanager.dividemessage (content);
for (String text:dividecontents) {
Smsmanager.sendtextmessage ("150xxxxxxxx", null, Text, SENTPI, DELIVERPI);
}

Second: The system to send SMS function; The main code is as follows:
Copy Code code as follows:

Uri uri = uri.parse ("smsto:10010");
Intent it = new Intent (intent.action_sendto, URI);
It.putextra ("Sms_body", "102");
Activity.startactivity (IT);

This is mainly about the first method, most of which comes from the Internet:
Get Message Manager
Copy Code code as follows:

Smsmanager Smsmanager = Smsmanager.getdefault ();

Split SMS content (SMS length limit)
Copy Code code as follows:

list<string> dividecontents = smsmanager.dividemessage (content);

Send the split content
Copy Code code as follows:

list<string> dividecontents = smsmanager.dividemessage (content);
for (String text:dividecontents) {
Smsmanager.sendtextmessage ("150xxxxxxxx", null, Text, SENTPI, DELIVERPI);
}

Handling the Send status returned
Copy Code code as follows:

String sent_sms_action = "Sent_sms_action";
Intent sentintent = new Intent (sent_sms_action);
Pendingintent SENTPI = pendingintent.getbroadcast (context, 0, sentintent,
0);
Register the broadcast receivers
Context.registerreceiver (New Broadcastreceiver () {
@Override
public void OnReceive (context _context, Intent _intent) {
Switch (Getresultcode ()) {
Case ACTIVITY.RESULT_OK:
Toast.maketext (Context,
"SMS Send Success", 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:
Break
}
}
}, New Intentfilter (sent_sms_action));

Processing the received status returned
Copy Code code as follows:

String delivered_sms_action = "Delivered_sms_action";
Create the Deilverintent parameter
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,
"Recipient has successfully received", Toast.length_short)
. Show ();
}
}, New Intentfilter (delivered_sms_action));

parameter description of sending SMS
Copy Code code as follows:

Smsmanager.sendtextmessage (destinationaddress, scaddress, text, sentintent, deliveryintent)

--destinationaddress: Target phone number
--scaddress: SMS Center number, test can not fill
--Text: SMS Content
--sentintent: Send--> China Mobile--> send failure--> return send success or failure signal--> subsequent processing that is, this intent to package the message sent status information
--deliveryintent: Send--> China Mobile--> to send a successful--> back to the other side whether received this information--> follow-up processing that is: this intention to wrap the message is received by the other side of the status information (the supplier has been sent successfully, But the other side did not receive it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.