1:strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">短信发送器</string>
<string name="mobile">请输入手机号</string>
<string name="content">短信内容</string>
<string name="btn">发送短信</string>
<string name="success">短信发送成功</string>
</resources>
2:main.xml
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mobile"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mobile"
/>
<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/content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn"
android:id="@+id/btn"
/>
</LinearLayout>
3:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pnova.sms"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SmsActity"
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>
4:SmsActity.java
package com.pnova.sms;
import java.util.ArrayList;
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;
/**
* 短信发送器
* @author qian.long
*
*/
public class SmsActity extends Activity {
private EditText mobileText;
private EditText contentText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.mobileText = (EditText) this.findViewById(R.id.mobile);
this.contentText = (EditText) this.findViewById(R.id.content);
Button btn = (Button) this.findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String mobile = mobileText.getText().toString();
String content = contentText.getText().toString();
//短信发送管理类android.telephony.SmsManager
SmsManager smsManager = SmsManager.getDefault();
//将可能短信字数过多时拆分
ArrayList<String> texts = smsManager.divideMessage(content);
for (String text : texts) {
//发送短信
smsManager.sendTextMessage(mobile, null, text, null, null);
}
//吐司模式告诉用户短信发送成功
//makeText(Context context//应用的上下文, int resId//提示内容的id, int duration//显示的时长)
Toast.makeText(SmsActity.this, R.string.success, Toast.LENGTH_LONG);
}
});
}
}