Java Learning Lesson 13th, Android implementation of the ability to send text messages

Source: Internet
Author: User

1. Interface layout

Interface code:

<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/number"/> <EditText android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "@null"Android:id= "@+id/number2"/> <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/content"/> <EditText android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:minlines= "3"Android:id= "@+id/content"Android:hint= "@null"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/button"Android:id= "@+id/button"/></linearlayout>

2. Main code

 Packagecom.example.sms;Importjava.util.ArrayList;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.telephony.gsm.SmsManager;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {EditText numbertext;    EditText ContentText;        Button button; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Numbertext= (EditText) This. Findviewbyid (R.id.number2); ContentText= (EditText) This. Findviewbyid (r.id.content); Button= (Button) This. Findviewbyid (R.id.button); Button.setonclicklistener (NewButtonclicklistener ()); }        Private Final classButtonclicklistenerImplementsview.onclicklistener{@SuppressWarnings ("Deprecation") @Override Public voidOnClick (View v) {//TODO Auto-generated method stubsString number =Numbertext.gettext (). toString (); String content=Contenttext.gettext (). toString (); Smsmanager Manager=Smsmanager.getdefault (); //If the text message is too long, can be automatically split, if the number of words more than 70, need to split into multiple SMS sentarraylist<string> texts =manager.dividemessage (content);  for(String text:texts) {//Send SMS InstructionsManager.sendtextmessage (number,NULL, text,NULL,NULL); //The last parameter is the broadcast intent that the SMS has sent, and the last parameter is the broadcast intent of the SMS message that the caller has received            }                        //Display sent successfullyToast.maketext (mainactivity. This, R.string.success, Toast.length_long). Show (); }    }}

3.string.xml

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string name= "App_name" >sms</string >    <string name= "Hello_world" >hello world!</string>    <string name= "number" > Please enter your mobile phone number:< /string>    <string name= "Content" > Please enter content:</string>    <string name= "button" > Send </string >    <string name= "Success" > Send success </string></resources>

4. Request permission (text message)

Note: After the software point sends the button, there are three ways to prompt:

Java Learning Lesson 13th, Android implementation of the ability to send text messages

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.