首先有几点要注意的:
1、Toast(土司)的使用
Toast.makeText(this, "电话号码和短信内容不能为空!", Toast.LENGTH_LONG).show();
2、短信如果超过发送长度记得要分割
ArrayList<String> arrs = sms.divideMessage(str_content);
3、发送短信用的类:SmsManager
4、记得添加用户发送短信权限:android.permission.SEND_SMS
关键代码如下:
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.btn_cancel:
this.edt_number.setText("");
this.edt_content.setText("");
break;
case R.id.btn_send:
//valiad empty
String str_num = edt_number.getText().toString().trim();
String str_content = edt_content.getText().toString().trim();
if("".equals(str_num)||"".equals(str_content))
{
Toast.makeText(this, "电话号码和短信内容不能为空!", Toast.LENGTH_LONG).show();
return;
}
SmsManager sms = SmsManager.getDefault();
if(str_content.length()>150)
{
ArrayList<String> arrs = sms.divideMessage(str_content);
for(String str:arrs)
{
sms.sendTextMessage(str_num, null, str_content, null, null);
}
}
Toast.makeText(this, "已发送成功!", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}