Two Android simulator Interactive SMS program demo _android

Source: Internet
Author: User
Tags constant gettext

First, the creation of Android project

Project Name:sendmessage

buildtarget:android2.2

Application name: Send SMS

Package name:com.sms.Activity

Create Activity:sendmessage

Min SDK Version:8

Second, the editorial project

1. Edit String strings. xml file contents are:

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "Hello" > Please enter your mobile number:</string>
<string name= "app_name" > Send SMS </string>
<string name= "Content" > Please enter the information content:</string>
<string name= "Send" > Send </string>
</resources>

2. Edit layout layout file Main.xml file contents are:

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "vertical" android:layout_width= "fill_parent"
android:layout_height= "Fill_parent" >
<!--Please enter your mobile phone number tag-->
<textview android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:text= "@string/hello"/>

<!--mobile number edit box-->
<edittext android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:id= "@+id/mobile"/>

<!--Please enter the information content label-->
<textview android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:text= "@string/content"/>

<!--information content edit box-->
<edittext android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:minlines= "3"
Android:id= "@+id/message"/>

<!--send button-->
<button android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" android:text= "@string/send"
Android:id= "@+id/send"/>
</LinearLayout>

Notice that we added the Android:id property to the Phone Number entry box and to the call button. such as the android:id= "@+id/mobile" of the phone number input box, the @ Code R.JAVA,+ID code adds the ID static inner class, and the mobile representative adds a constant member to the ID class. ADT will automatically generate constant values for us.

Android:minlines sets the minimum number of rows for the information content edit box.

3. Edit Java file Call.java content:

Copy Code code as follows:

Package com.sms.activity;

Import java.util.List;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.telephony.SmsManager;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.util.Log;

public class SendMessage extends activity {

/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Get button based on ID
Button button = (button) This.findviewbyid (r.id.send);
Register button click event
Button.setonclicklistener (New Onclicklistener () {

public void OnClick (View v) {

Get cell phone number edit box based on ID
EditText Mobiletext = (edittext) Findviewbyid (r.id.mobile);

Get cell phone number
String mobile = Mobiletext.gettext (). toString ();

Get Information content edit box based on ID
EditText MessageText = (edittext) Findviewbyid (r.id.message);

Get information Content
String message = Messagetext.gettext (). toString ();

Mobile operators allow a limited amount of byte data to be sent each time, and we can use the SMS tool provided by Android.
if (message!= null) {
Smsmanager SMS = Smsmanager.getdefault ();

If the message does not exceed the limit length, then returns a length list.
list<string> texts = sms.dividemessage (message);

for (String text:texts) {
Sms.sendtextmessage (mobile, NULL, text, NULL, NULL);
LOG.I ("SMS", "Send a Message");
}
}
}
});
}
}

Sms.sendtextmessage (destinationaddress, scaddress, text, Sentintent, deliveryintent):

Destinationaddress: The phone number of the receiving party

Scaddress: SMS Center number, test can not fill out.

Text: Information content

Sentintent: Send a successful receipt, which will be described in detail later.

Deliveryintent: Receive a successful receipt, which will be described in more detail later.

LOG.I (): Write Log for easy debugging.

4. Edit Androidmanifest.xml Content:

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.changcheng.activity" android:versioncode= "1"
Android:versionname= "1.0" >
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<activity android:name= ". SendMessage "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>
&LT;USES-SDK android:minsdkversion= "8"/>

<!--register to send SMS permission, must add, otherwise no permission to send text messages-->
<uses-permission android:name= "Android.permission.SEND_SMS"/>

</manifest>

Register to send SMS permission, if not registered this, will not use the system to send SMS function. Later in our application development, there is a need to register in this file with the functionality of the system. We can see what the Android Help book does. (.../android-sdk-windows/docs/reference/android/manifest.permission.html)

Third, start the simulator

Who do we send text messages to? We can start two simulators. Use an emulator to send information to another emulator. First, we use the phone icon on the toolbar to add a Android2.1 simulator and a name.

Before starting two simulators, we need the simulator to "receive the signal". If our machine is networked, when the simulator is started, the main interface shows the signal strength next to the word 3G, indicating that the simulator has been able to receive the signal. If our machines cannot be networked, set their own IP addresses, gateways, and DNS servers to the same values, such as all set to 192.168.0.100. If our machine is under the LAN, but not connected, then set its own gateway and DNS to route IP can, in general, the route of IP is 192.168.0.1.

OK, now we're going to start two simulators!

Four, send text messages

After we start the simulator, we can see that the title bar of the emulator window has 5554 and 5556 words on it. This is the port that the simulator listens to, namely--127.0.0.1:5554.

Right-click on the project, Run as Android application, select one of the simulators. For example, a simulator with a port of 5554 is selected. OK, the program is loaded into the emulator and will be run automatically.

We enter 5556 in the Phone number edit box (receiver side Simulator port number), click the Send button!

OK, did you see the effect? 5556 main interface, showing signal strength next to the display received new SMS.

V. Use the ADT plugin to send SMS to the simulator

If the machine is too slow to start two emulators, we can start only one emulator. Then open the emulator Control Panel in the menu Windows->show view->other->android->emulator control.

In the Telephony Actions group box, the voice is a call, and SMS is sending SMS. Incoming number is the port of the simulator, and we can also use this feature to call our simulator or send a text message.

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.