事先准备:
开通短信权限 <uses-permission android:name="android.permission.SEND_SMS"/>
第一:直接发送短信
/**
* 直接调用短信接口发短信
* @param phoneNumber 电话号码
* @param message 短信内容
*/
public void sendSMS(String phoneNumber, String message) {
// 处理返回的接收状态
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
// create the deilverIntent parameter
Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI = PendingIntent.getBroadcast(this, 0,
deliverIntent, 0);
// 处理返回的发送状态
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent,
0);
// 获取短信管理器
android.telephony.SmsManager smsManager = android.telephony.SmsManager
.getDefault();
// 拆分短信内容(手机短信长度限制)
List<String> divideContents = smsManager.divideMessage(message);
for (String text : divideContents) {
smsManager.sendTextMessage(phoneNumber, null, text, sentPI,
deliverPI);
}
}
第二:调用系统发送短信(须点发送按钮)
// 发送短信
Uri uri = Uri.parse("smsto:18982120000");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.putExtra("sms_body", "自动骚扰");
intent.setType("vnd.android-dir/mms-sms");
intent.setData(uri);
MainActivity.this.startActivity(intent);