界面布局:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical“ android:layout_width="fill_parent“ android:layout_height="fill_parent" >
<TextViewandroid:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="@string/inputmobile"/>
<EditTextandroid:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/mobile"/>
<TextViewandroid:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="@string/content"/>
<EditTextandroid:layout_width="fill_parent" android:layout_height="wrap_content"
android:minLines="3"
android:id="@+id/content"/>
<Buttonandroid:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"/>
</LinearLayout>
权限声明
<uses-permissionandroid:name="android.permission.SEND_SMS"
Activity主要代码:
Stringmobile = mobileView.getText().toString();
Stringcontent = contentView.getText().toString();
SmsManager smsManager =SmsManager.getDefault();
PendingIntent sentIntent =PendingIntent.getBroadcast(SMSSender.this, 0, new Intent(), 0);
//如果字数超过70,需拆分成多条短信发送
List<String>msgs = smsManager.divideMessage(content);
for(Stringmsg : msgs){
smsManager.sendTextMessage(mobile, null,msg, sentIntent, null);
//最后二个参数为短信已发送的广播意图,最后一个参数为短信对方已收到短信的广播意图
}
Toast.makeText(SMSSender.this,"短信发送完成", Toast.LENGTH_LONG).show();