1.在res/layout/activity_main.xml文件中,编辑界面控件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sms.MainActivity"
tools:ignore="MergeRootFrame"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/str_input_phone_number"/>
<EditText
android:id="@+id/phone_number_editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/str_input_sms_content"/>
<EditText
android:id="@+id/sms_content_editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/send_sms_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_send_sms" />
</LinearLayout>
2.在res/values/strings.xml中,定义需要用到的字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SMS</string>
<string name="action_settings">Settings</string>
<string name="str_input_phone_number">请输入手机号码:</string>
<string name="str_input_sms_content">请输入内容:</string>
<string name="str_send_sms">发送短信</string>
<string name="str_remind_input_phone_number">请输入手机号码:</string>
<string name="str_remind_sms_send_finish">发送完成</string>
</resources>
3.在MainnActivity.java中,编写发送短信的逻辑代码
package com.example.sms;
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.widget.Toast;
public class MainActivity extends Activity {
private EditText phone_number_editText;
private EditText sms_content_editText;
private Button send_sms_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phone_number_editText = (EditText) findViewById(R.id.phone_number_editText);
sms_content_editText = (EditText) findViewById(R.id.sms_content_editText);
send_sms_button = (Button) findViewById(R.id.send_sms_button);
send_sms_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String phone_number = phone_number_editText.getText()
.toString().trim();
String sms_content = sms_content_editText.getText().toString()
.trim();
if (phone_number.equals("")) {
Toast.makeText(MainActivity.this,
R.string.str_remind_input_phone_number,
Toast.LENGTH_LONG).show();
} else {
SmsManager smsManager = SmsManager.getDefault();
if (sms_content.length() > 70) {
List<String> contents = smsManager
.divideMessage(sms_content);
for (String sms : contents) {
smsManager.sendTextMessage(phone_number, null,
sms_content, null, null);
}
} else {
smsManager.sendTextMessage(phone_number, null,
sms_content, null, null);
}
Toast.makeText(MainActivity.this,
R.string.str_remind_sms_send_finish,
Toast.LENGTH_SHORT).show();
}
}
});
}
}
4.在AndroidManifest.xml中,声明发短信权限"android.permission.SEND_SMS"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sms.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>
</manifest>
显示效果: