Android sends SMS messages
Layout layout file main. xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/mobile" /> <EditText android:id="@+id/mobile" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content" /> <EditText android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="3" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /></LinearLayout>
Strings. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> Hello smsactivity! </String> <string name = "app_name"> text message sender </string> <string name = "mobile"> enter the mobile phone number </string> <string name = "content"> enter the text message content </string> <string name = "button"> send SMS </string> <string name = "success"> message sent successfully </string> </ resources>
Androidmanifest. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. cloud. android. SMS "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 7 "/> <! -- The program adds the SMS sending permission --> <uses-Permission Android: Name = "android. permission. send_sms "/> <application Android: icon =" @ drawable/agree_logo "Android: Label =" @ string/app_name "> <activity Android: label = "@ string/app_name" Android: Name = ". sms_cloudactivity "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> </Application> </manifest>
Sms_cloudactivity.java
Package COM. cloud. android. SMS; import Java. util. list; import android. app. activity; import android. OS. bundle; import android. telephony. smsmanager; import android. view. view; import android. widget. button; import android. widget. edittext; import android. widget. toast;/*** text message sender (manual) * @ author Li haiyun * @ email CloudComputing.cc@gmail.com * @ date 2012-02-26 00:06:22 */public class sms_cloudactivity extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button = (button) This. findviewbyid (R. id. button); button. setonclicklistener (new view. onclicklistener () {@ override public void onclick (view v) {// get the mobile phone number and content edittext mobiletext = (edittext) findviewbyid (R. id. mobile); edittext contenttext = (edittext) findviewbyid (R. id. content); string mobile = mobiletext. gettext (). tostring (); string content = contenttext. gettext (). tostring (); // send SMS smsmanager = smsmanager. getdefault (); List <string> texts = smsmanager. dividemessage (content); // if the number of Chinese characters exceeds the limit, the system automatically splits the message for (string text: texts) {smsmanager. sendtextmessage (mobile, null, text, null, null);} // toast notification with toast. maketext (sms_cloudactivity.this, R. string. success, toast. length_long ). show ();}});}}