Android's third application-text message sender

Source: Internet
Author: User

I,

II,

Because the application needs to use the text message service of the mobile phone, you must add the text message service permission to the androidmanifest. xml file in the list file:

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "cn. itcast. SMS"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
....
<Uses-SDK Android: minsdkversion = "4"/>
<Uses-Permission Android: Name = "android. Permission. send_sms"/>
</Manifest>

3. layout Interface

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".SmsActivity" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/phone_num" />    <EditText        android:id="@+id/phonenum"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView1"        android:layout_below="@+id/textView1"        android:layout_marginTop="15dp"        android:ems="10"        android:inputType="phone" >        <requestFocus />    </EditText>        <TextView         android:id="@+id/sms_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/phonenum"        android:layout_marginTop="15dp"        android:text="@string/sms_content"                />    <EditText        android:id="@+id/smsContent"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/sms_content"        android:layout_below="@+id/sms_content"        android:layout_marginTop="26dp"        android:ems="10"        android:inputType="textMultiLine" />    <Button        android:id="@+id/sendButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/smsContent"        android:layout_below="@+id/smsContent"        android:layout_marginTop="58dp"        android:text="@string/send_button" /></RelativeLayout>

Iv. string attributes referenced in the layout Interface

<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "app_name"> lession01-Sms </string> <string name = "action_settings"> Settings </string> <string name = "hello_world"> Hello world! </String> <string name = "phone_num"> enter the phone number </string> <string name = "sms_content"> enter the text message content </string> <string name = "send_button"> send message </string> </resources>

5. smsactivity code:

Package COM. example. lession01_sms; import android.net. uri; import android. OS. bundle; import android. app. activity; import android. app. pendingintent; import android. content. intent; import android. telephony. smsmanager; import android. view. menu; import android. view. view; import android. widget. button; import android. widget. edittext; import android. widget. toast; public class smsactivity extends activity {// declare the component publi C button sendbutton; Public edittext smscontent; Public edittext phonenum; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // sets the displayed view setcontentview (R. layout. activity_sms); // obtain the component sendbutton = (button) findviewbyid (R. id. sendbutton); smscontent = (edittext) findviewbyid (R. id. smscontent); phonenum = (edittext) findviewbyid (R. id. phonenum); // register the event sendbutto for the button N. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {// obtain the phone number string phone_num = phonenum. gettext (). tostring (); // create the intent object intent = new intent (intent. action_send); intent. putextra (intent. extra_stream, Uri. parse ("file: // sdcard/javasjpg"); intent. putextra ("Address", phone_num); intent. putextra ("exit_on_sent", true); intent. putextra ("subject", "Subject: I love you !!!!!! "); Intent. putextra ("sms_body", "content: XXXX"); intent. settype ("image/JPEG"); startactivity (intent) ;}});} public void send () {// obtain the phone number string phone_num = phonenum. gettext (). tostring (); // get the text message content string sms_content = smscontent. gettext (). tostring (); // get the SMS manager object smsmanager = smsmanager. getdefault (); // intent object pendingintent = pendingintent. getbroadcast (smsactivity. this, 0, new intent (), 0); // smsmanager for sending information. sendtextmessage (phone_num, null, sms_content, pendingintent, null); // toast. maketext (smsactivity. this, "sms sent successfully", toast. length_long ). show () ;}@ override public Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. SMS, menu); Return true ;}}

Vi. Test Procedure

1> run this application in eclipse
2> in the DOS window, go to the tools directory of the android SDK installation path, and run the following command to enable an android simulator:
Emulator-data csdn
Note: icsdn is a user data access file. If the file does not exist, this file is created in the tools directory by default.

3> enter the displayed phone number in the mobile phone number of the text message sender

Note: currently, the android system does not support Chinese text messages, so Garbled text messages are sent. This problem will be solved in the future.

 

 

 

 

 

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.