Android small function implementation-send SMS, android send SMS

Source: Internet
Author: User

Android small function implementation-send SMS, android send SMS
Create an Android project.


1 Layout
Open main. xml and modify the content as follows:

 <TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="@string/number" />  <EditText   android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:inputType="text"   android:id="@+id/number"/>  <TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="@string/content" />  <EditText   android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:inputType="text"   android:minLines="3"  android:id="@+id/content"/>  <Button   android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="@string/button"  android:id="@+id/button" />






Binary definition string
Open strings. xml and add the following content:
<String name = "number"> enter the mobile phone number </string> <string name = "content"> enter the text message content </string> <string name = "button"> send SMS </string> <string name = "success"> sent successfully </string>


3. Response to click events
Open MainActivity. java and add the following code:
Public EditText numberText; public EditText contentText; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); numberText = (EditText) this. findViewById (R. id. number); contentText = (EditText) this. findViewById (R. id. contentText); Button button = (Button) this. findViewById (R. id. button); button. setOnClickListener (new ButtonClickListener ();} private final class ButtonClickListener implements View. onClickListener {public void onClick (View v) {String number = numberText. getText (). toString (); String content = contentText. getText (). toString (); SmsManager manger = SmsManager. getDefault (); // The number of characters in the text message. If 36 characters are left blank, ArrayList <String> texts = manger. divideMessage (content); for (String text: texts) {// parameter: Number, center address, content, sending status, whether the recipient receives status manger. sendTextMessage (number, null, text, null, null);} // notify the user Toast after the SMS is sent. makeText (getApplicationContext (), R. string. success, Toast. LENGTH_LONG ). show ();}}


4. Add Permissions

Add permissions to Manifest. xml:

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



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.