Demo of two android emulators sending text messages to each other
- 2020-05-19 05:48:36
- OfStack
1. Create the Android project
Project name:SendMessage
BuildTarget:Android2.2
Application name: send text messages
Package name:com.sms.Activity
Create Activity:SendMessage
Min SDK Version:8
2. Editorial engineering
1. Edit the string strings. xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello"> Please enter your mobile phone number: </string>
<string name="app_name"> Send a text message </string>
<string name="content"> Please enter information content: </string>
<string name="send"> send </string>
</resources>
2. Edit layout layout file main.xml file contents are 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 the mobile phone number label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<!-- Cell phone number edit box -->
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/mobile" />
<!-- Please enter 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 input box and the call button. For example, in the phone number input box, android:id= "@+id/mobile", @ code R.java, +id code adds id static inner class, and mobile represents adding a constant member to id class. ADT will automatically generate constant values for us.
android:minLines sets the minimum number of lines in the information content edit box.
3. Edit java file Call.java content:
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 when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// According to the ID To get the button
Button button = (Button) this.findViewById(R.id.send);
// Register button is clicked
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// According to the ID Get the phone number edit box
EditText mobileText = (EditText) findViewById(R.id.mobile);
// Get the phone number
String mobile = mobileText.getText().toString();
// According to the ID Get information content edit box
EditText messageText = (EditText) findViewById(R.id.message);
// Get information content
String message = messageText.getText().toString();
// Mobile carriers allow limited bytes of data to be sent at a time that we can use Android Provide us with SMS tools.
if (message != null) {
SmsManager sms = SmsManager.getDefault();
// If the message does not exceed the length limit, it is returned 1 A length of 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: phone number of the recipient
scAddress: SMS center number, can not be filled in the test.
text: information content
sentIntent: return receipt for successful delivery, more on this later.
DeliveryIntent: whether or not the receipt was successful will be described in detail later.
Log.i () : writes to the log for easy debugging.
4. Edit AndroidManifest.xml content:
<?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>
<uses-sdk android:minSdkVersion="8" />
<!-- Sign up to send SMS messages , Must add, otherwise no permission to send SMS -->
<uses-permission android:name="android.permission.SEND_SMS" />
</manifest>
Register the permission to send SMS. If you do not register this, you will not be able to use the system's SMS function. Later in our application development, anyone who USES system functions must register in this file. We can see what the Android help book has to offer. (... / android - sdk - windows/docs/reference/android/Manifest permission. html)
3. Start the simulator
Who are we texting? We can start two emulators. Use one emulator to send a message to the other. First, we use the phone icon in the toolbar to add an Android 2.1 simulator and remember another name.
Before starting both simulators, we need the simulators to "pick up the signal." If our machine is connected to the Internet, after starting the simulator, there will be a message of 3G next to the signal strength displayed on the main interface, indicating that the simulator has received the signal. If our machine is not connected to the Internet, set our IP address, gateway, and DNS server to the same value, such as 192.168.0.100. If our machine is on a local area network but not connected to the Internet, then we can set our gateway and DNS as IP for routing. In general, IP for routing is 192.168.0.1.
OK, now let's start two emulators!
4. Send text messages
After we start the simulator, we can see the words 5554 and 5556 in the title bar of the simulator window. This is the port on which the simulator is listening -- 127.0.0.1:5554.
Right-click on the project, Run As Android Application, and select one of the emulators. For example, an emulator with port 5554 was selected. OK, the program is loaded into the simulator and will be run automatically.
We enter 5556 (the port number of the receiver simulator) in the phone number edit box and click the send button!
OK, do you see the effect? 5556 main interface, display signal strength next to the display received new messages.
5. Use the ADT plugin to send SMS messages to the emulator
If the machine is too slow to start two emulators, we can start only one. Then on the menu windows- > show view- > other- > Android- > Emulator Control open the Emulator Control panel.
In the Telephony Actions grouping box, Voice is for calling and SMS is for sending SMS messages. Incoming number is the port number of the emulator, and we can also use this feature to make phone calls or send text messages to our emulator.