Android Phone SMS Send

Source: Internet
Author: User

Just learn Android soon, recently Micaela sent me a video, is how to make Android phone SMS transmitter, on the one hand I feel very interesting, the other side can accompany her together, today finished, special to summarize, although said relatively simple, but there is a summary of the necessary.

SMS transmitter is mainly used for Android text messages sent, the main interface is to enter the sender's number and the message to send the content, the main interface has two TextView and two edittext there is a button (in the Import android.widget.*), here is the layout file:

<?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/number"

/>

<edittext

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

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:minlines= "3"

Android:id= "@+id/context"

/>

<button

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:text= "@string/button"

Android:id= "@+id/button"

/>

</LinearLayout>

The above layout file uses a string value that needs to be set in Value/string.xml and the main code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>

<resources>

<string name= "App_name" >smsTest</string>

<string name= "Action_settings" >Settings</string>

<string name= "Content" > Please enter SMS content </string>

<string name= "Number" > Please enter your mobile code </string>

<string name= "button" > Send information </string>

<string name= "Success" > Send success </string>

</resources>

The above is the layout, can go to the AVD to try whether or not successful, but we want to complete this SMS transmitter also need to be in the main program. Java write the corresponding processing.

Package com.example.smstest;

Import Android.os.Bundle;

Import android.app.Activity;

Import Android.view.Menu;

Import Android.telephony.gsm.SmsManager;

Import Android.view.View;

Import Android.widget.Button;

Import Android.widget.EditText;

Import Android.widget.Toast;

Import java.util.arraylist;//above is the required package

@SuppressWarnings ("deprecation")

public class Mainactivity extends Activity {

Private EditText numbertext;//This variable is used to accept the number entered in the layout

Private EditText contenttext;//This variable is used to accept the information entered in the layout

@Override

protected void OnCreate (Bundle savedinstancestate) {//Such programs will start from this song in the execution

Super.oncreate (savedinstancestate);//Call the OnCreate method in the parent activity

Setcontentview (R.layout.activity_main);//Execute Layout file

Numbertext = (EditText) This.findviewbyid (R.id.number); Accept the number in the input box in the layout

ContentText = (EditText) This.findviewbyid (r.id.context);//Accept the information in the input box in the layout

Button button = (button) This.findviewbyid (R.id.button);//Use button

Button.setonclicklistener (New ButtonClick ());//Set the content of the button response

}

Private Final class ButtonClick implements View.onclicklistener

{

@Override

public void OnClick (View arg0) {

String number = Numbertext.gettext (). toString ();//accept numbers from the input box in the layout

String content = Contenttext.gettext (). toString ();

Smsmanager manger = Smsmanager.getdefault ();//Set up SMS Management Object

Arraylist<string> texts=manger.dividemessage (content);//If the text message is too long than the maximum value of a text message is divided into multiple text messages sent

for (String text:texts) {

Manger.sendtextmessage (number, NULL, text, NULL, NULL);//Send a message (the first is the destination, the third is the message content)

}

Toast.maketext (Getapplicationcontext (), r.string.success, Toast.length_long). Show ();

Message sent successfully with Toast prompt

}

}

@Override

public boolean Oncreateoptionsmenu (Menu menu) {

Inflate the menu; This adds items to the action bar if it is present.

Getmenuinflater (). Inflate (R.menu.main, menu);

return true;

}

}

This is OK, can be sent in two simulator (Chinese characters will be garbled but the phone will not) 650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" spacer.gif "/>

But this still can not be used in the mobile phone, because the use of the phone is to have permission to use, such as allowing it to deduct the charges, then get permission to modify the/smstest/androidmanifest.xml to add a permission code

<?xml version= "1.0" encoding= "Utf-8"?>

<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"

Package= "Com.example.smstest"

Android:versioncode= "1"

Android:versionname= "1.0" >

<uses-sdk

android:minsdkversion= "6"

android:targetsdkversion= "Ten"/>

<application

Android:allowbackup= "true"

android:icon= "@drawable/ic_launcher"

Android:label= "@string/app_name"

Android:theme= "@style/apptheme" >

<activity

Android:name= "Com.example.smstest.MainActivity"

Android:label= "@string/app_name" >

<intent-filter>

<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

</application>

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

</manifest>

Red body Word is added to, the rest is automatically generated, to this SMS transmitter OK.

2014/04/03 14:44


This article is from the "Wangle-frank" blog, make sure to keep this source http://8119256.blog.51cto.com/8109256/1631603

Android Phone SMS Send

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.