<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/number" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="text" android:id="@+id/number"/> <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:inputType="text" android:minLines="3" android:id="@+id/content"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" />
<string name="number">请输入手机号</string> <string name="content">请输入短信内容</string> <string name="button">发送短信</string> <string name="success">发送成功</string>
public EditText numberText;
public EditText contentText;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
numberText = (EditText)this.findViewById(R.id.number);
contentText = (EditText)this.findViewById(R.id.contentText);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new ButtonClickListener());
}
private final class ButtonClickListener implements View.OnClickListener{
public void onClick(View v){
String number = numberText.getText().toString();
String content = contentText.getText().toString();
SmsManager manger = SmsManager.getDefault();
//分割短信字数,如果多余36字分条发送
ArrayList<String> texts=manger.divideMessage(content);
for(String text:texts){
// 参数:号码,中心地址,内容,发送状态,对方是否收到状态
manger.sendTextMessage(number, null, text, null, null);
}
//短信发送完毕,通知用户
Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show();
}
}在Manifest.xml中添加权限:
<uses-permission android:name="android.permission.SEND_SMS"/>
原文:http://blog.csdn.net/xufeng0991/article/details/43983303