Implementation of simulating android SMS senders
AndroidManifest. xml configuration file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ljq.sms"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".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-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>
Main. xml layout File
<? 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">
<RelativeLayout android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<TextView android: layout_width = "115dip"
Android: layout_height = "wrap_content"
Android: text = "Enter your mobile phone number"
Android: id = "@ + id/cancelabel"/>
<EditText android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_toRightOf = "@ id/cancelabel"
Android: text = "5556"
Android: id = "@ + id/mobile"/>
</RelativeLayout>
<TextView android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Enter text message content"/>
<EditText android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: minLines = "3"
Android: text = "I am a teacher! "
Android: id = "@ + id/content"/>
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "send"
Android: id = "@ + id/button"/>
</LinearLayout>
MainActivity class
Package com. ljq. sms;
Import java. util. ArrayList;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. telephony. SmsManager;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. Toast;
Public class MainActivity extends Activity {
Private EditText mobileText = null;
Private EditText contentText = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MobileText = (EditText) findViewById (R. id. mobile );
ContentText = (EditText) findViewById (R. id. content );
Button button = (Button) findViewById (R. id. button );
Button. setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
String mobile = mobileText. getText (). toString ();
String content = contentText. getText (). toString ();
// Obtain the default SMS manager in the android system
SmsManager manager = SmsManager. getDefault ();
// If the text message content is too long, split the text message content
ArrayList <String> texts = manager. divideMessage (content );
For (String text: texts ){
// The first parameter is the recipient's mobile phone number.
// The second parameter is the SMS center number, which is generally set to null.
// The third parameter is the text message content.
// The fourth parameter: sentIntent determines whether the text message is successfully sent. If you do not have a SIM card or the network is interrupted, you can use this intent to determine.
// Note whether the "send" action is successful. In other words
// The Fifth parameter: The deliveryIntent is received when the text message is sent to the recipient. That is, the result after "sending" is emphasized.
// This means that sentIntent and deliveryIntent are activated only when the message "sent successfully" and "received by the recipient. This is equivalent to the delay in Intent execution.
Manager. sendTextMessage (mobile, null, text, null, null );
}
// Toast. makeText (getApplicationContext (), "sent successfully", Toast. LENGTH_LONG). show ();
Toast. makeText (MainActivity. this, "sent successfully", Toast. LENGTH_LONG). show ();
}
});
}
}
Running result