Android Beginner Program-SMS Transmitter

Source: Internet
Author: User

Small project is to take advantage of Chase, this is to achieve the message of the sending function, things are very simple, but it is worth learning at that time.

As follows:

Specific steps:

  1. First, write the page, the code below, there is no point. There is a weight (weight) in the LinearLayout layout, proportionally allocated size

1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Tools:context=". Mainactivity " >6     <LinearLayout7         Android:id= "@+id/linear_layout"8 Android:layout_width= "Match_parent"9 Android:layout_height= "Wrap_content"Ten android:orientation= "Horizontal" One Android:paddingtop= "5dip" A         > -         <EditText -         Android:id= "@+id/edit_number" the Android:layout_width= "0dip" - Android:layout_height= "Wrap_content" - Android:layout_weight= "1" - Android:hint= "@string/contact_number" + Android:inputtype= "Phone"/> -     <Button +         Android:id= "@+id/btn_send" A Android:layout_width= "Wrap_content" at Android:layout_height= "Wrap_content" - Android:text= "@string/btn_send_sms"/> -     </LinearLayout> -      -  -     <EditText in         Android:id= "@+id/edit_content" - Android:layout_width= "Match_parent" to Android:layout_height= "Wrap_content" + Android:lines= "5" - Android:layout_below= "@id/linear_layout" the Android:hint= "@string/sms_content"/> * </Relativelayout>

2. Look at the Java code again

1  Public classMainactivityextendsActivityImplementsonclicklistener{2 3     PrivateEditText editcontent;4     PrivateEditText Editnumber;5 6 @Override7     protected voidonCreate (Bundle savedinstancestate) {8         Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_main);TenEditcontent =(EditText) Findviewbyid (r.id.edit_content); OneEditnumber =(EditText) Findviewbyid (r.id.edit_number); A          -Findviewbyid (r.id.btn_send). Setonclicklistener ( This); -     } the  - @Override -      Public voidOnClick (view view) { -         Switch(View.getid ()) { +          CaseR.id.btn_send: -             //SMS Manager +Smsmanager manager=Smsmanager.getdefault (); AString number=Editnumber.gettext (). toString (); atString content=Editcontent.gettext (). toString (); -              -Manager.sendtextmessage (number,NULL, Content,NULL,NULL); -              Break; -  -         default: in              Break; -         } to     } +}

Text message This way does not call the system as the dialer, directly call Smsmanager SMS Manager to send text messages, where Sendtextmessage (string destinationaddress, String scaddress, String Text, Pendingintent sentintent, Pendingintent deliveryintent) method four parameters, the first parameter is a string type, direct translation is the target address, in fact, is the recipient's mobile phone number, The second parameter is also a string type, is the SMS Service center, is generally null, is the current default SMS service center (not to understand). The third parameter is also the content of the text message, the fourth parameter is a pendingintent, when the message is issued, the success or failure of the information report through the Pendingintent to broadcast, temporarily null. The fourth parameter is also a pendingintent, and when a message is sent to the recipient, the Pendingintent is broadcast and temporarily null.
3. Finally, the right to send text messages to the app, add the following code to the manifest file

<android:name= "Android.permission.SEND_SMS"/>

Finished, you can try, whether in the real machine or simulator, the code is not complex, this is learning something, the function can be better, that is later in the study in the added.
Please also give us a lot of advice!!!

Android Beginner Program-SMS Transmitter

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.