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 );}}