Getting started with Android: Use smsmanager to send text messages

Source: Internet
Author: User

We can call the short message service through intent, but it can also be sent through smsmanager;

I. Why do I need to develop a text message sender?

Although the app for sending text messages already exists in the Android system, it is convenient to integrate the SMS sending function when developing other apps.

Ii. develop SMS senders

Purpose: Install the application on the 5554 simulator and send it to the 5556 simulator;

Interface introduction:

Core code:

(1) smsmanager manager = smsmanager. getdefault (); // obtain the default message manager.

(2) arraylist <string> List = manager. dividemessage (string txt); // split long messages

(3) manager. sendtextmessage (string phone, null, string content, null, null); // send SMS

Add the following in androidmanifest. xml:

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

Mainactivity. Java

Package Org. xiazdong; import Java. util. arraylist; import android. app. activity; import android. OS. bundle; import android. telephony. smsmanager; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. widget. toast; public class mainactivity extends activity {private button BTN; private edittext phoneet, contextet; private onclicklistener listener = new onclicklistener () {@ overridepublic void onclick (view V) {string phone = phoneet. gettext (). tostring (); string context = contextet. gettext (). tostring (); smsmanager manager = smsmanager. getdefault (); arraylist <string> List = manager. dividemessage (context); // because there is a limit on the number of characters in a text message, you need to split the long text message for (string text: List) {manager. sendtextmessage (phone, null, text, null, null);} toast. maketext (getapplicationcontext (), "sent", toast. length_short ). show () ;};@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); BTN = (button) This. findviewbyid (R. id. BTN); phoneet = (edittext) This. findviewbyid (R. id. phonenumberet); contextet = (edittext) This. findviewbyid (R. id. contextet); BTN. setonclicklistener (listener );}}

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.