1.string xml代码
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="hello">Hello World, SmsActivity!</string>
<string name="app_name">短信发送器</string>
<string name="lab_number">请输入手机号</string>
<string name="lab_sms">请输短信内容</string>
<string name="lab_btn">发送短信</string>
<string name="sucess">发送成功</string> </resources>
2.mail xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/lab_number" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_number" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/lab_sms" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:id="@+id/txt_sms" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lab_btn"
android:id="@+id/btn_ok"
/> </LinearLayout>
3.SmsActivity.java 代码
package FosgeIT.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; /*
*
* @author YinRQ
* 短信模拟器
* 2013-07-05 09:09:44
*/ public class SmsActivity extends Activity { //定义窗口元素
private EditText txt_number;
private EditText txt_sms;
private Button btn_ok; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //初始化控件
txt_number = (EditText) this.findViewById(R.id.txt_number);
txt_sms = (EditText) this.findViewById(R.id.txt_sms);
btn_ok = (Button) this.findViewById(R.id.btn_ok); btn_ok.setOnClickListener(new ButtonClickListener());
} private final class ButtonClickListener implements View.OnClickListener{ public void onClick(View v) {
String number = txt_number.getText().toString();
String sms = txt_sms.getText().toString(); //获取SmsManager
SmsManager manager=SmsManager.getDefault();
//如果内容大于70字,则拆分为多条
ArrayList<String> texts=manager.divideMessage(sms);
//逐条发送短信
for(String text:texts) {
manager.sendTextMessage(number, null, text, null, null);
}
//发送结果提示
Toast.makeText(SmsActivity.this,R.string.sucess, Toast.LENGTH_LONG).show(); }}
}
4.发送短信权限 AndroidManifest.xml文件
<uses-permission android:name="android.permission.SEND_SMS"/>
效果:
关于SmsManager
SDK中的介绍:Manages SMS operations such as sending data, text, and pdu SMS messages. Get this object by calling the static method SmsManager.getDefault().
方法:
public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
destinationAddress: 收件人地址
scAddress: 短信中心号码,null为默认中心号码
sentIntent: 当消息发出时,成功或者失败的信息报告通过PendingIntent来广播。如果该参数为空,则发信程序会被所有位置程序检查一遍,这样会导致发送时间延长。
deliveryIntent: 当消息发送到收件人时,该PendingIntent会被广播。pdu数据在状态报告的extended data ("pdu")中。
如果收件人或者信息为空则抛出 IllegalArgumentException 。
public ArrayList<String> divideMessage (String text)
将大于70字的短信分割为多条。
参数:text the original message. Must not be null.
返回:an ArrayList of strings that, in order, comprise the original message
sendDataMessage 参数与上类似,只是用于发送Data。
sendMultipartTextMessage发送多条短信,发送内容必须是用divideMessage分割好了的。