要使用手机短信服务,在AndroidManifest.xml中必须添加短信服务权限
<uses-permission android:name="
android.permission.SEND_SMS"/><!--添加权限-->
主程序
public class MainActivity
extends Activity {
private EditText txtNo;
private EditText txtContent;
private Button btnSend;
/** Called when the activity is first created. */ @Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
txtNo = (EditText) findViewById(R.id.txtNo);
txtContent = (EditText) findViewById(R.id.txtContent);
btnSend = (Button) findViewById(R.id.btnSend);
btnSend.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
String strNo = txtNo.getText().toString();
String strContent = txtContent.getText().toString();
SmsManager smsManager = SmsManager.getDefault();
PendingIntent sentIntent = PendingIntent.getBroadcast(MainActivity.
this, 0,
new Intent(), 0);
//如果字数超过70,需拆分成多条短信发送 if (strContent.length() > 70) {
List<String> msgs = smsManager.divideMessage(strContent);
for (String msg : msgs) {
smsManager.sendTextMessage(strNo,
null, msg, sentIntent,
null);
}
}
else {
smsManager.sendTextMessage(strNo,
null, strContent, sentIntent,
null);
}
Toast.makeText(MainActivity.
this, "
短信发送完成", Toast.LENGTH_LONG).show();
}
});
}
}
参考资料:
http://www.cnblogs.com/livesoft/archive/2010/12/09/1901594.html用户系统信息:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; Tablet PC 2.0; .NET4.0E)