http://home.cnblogs.com/group/topic/49026.html
一、调起系统发短信功能,跳转到发送短信界面
二、发送短信
//直接调用短信接口发短信
SmsManager smsManager = SmsManager.getDefault();
List<String> divideContents = smsManager.divideMessage(content);
for (String text : divideContents) {
smsManager.sendTextMessage("150xxxxxxxx", null, text, sentPI, deliverPI);
}
解释:
1.获取短信管理器
- SmsManager smsManager = SmsManager.getDefault();
2.拆分短信内容(手机短信长度限制)
- List<String> divideContents = smsManager.divideMessage(content);
3.发送拆分后的内容
- List<String> divideContents = smsManager.divideMessage(content);
- for (String text : divideContents) {
- smsManager.sendTextMessage("150xxxxxxxx", null, text, sentPI, deliverPI);
- }
4.参数说明
destinationAddress:目标电话号码
scAddress:短信中心号码,测试可以不填
text: 短信内容
sentIntent:发送 -->中国移动 --> 中国移动发送失败 --> 返回发送成功或失败信号 --> 后续处理 即,这个意图包装了短信发送状态的信息。如果不为null,当短信发送成功或失败就广播。
deliveryIntent: 发送 -->中国移动 --> 中国移动发送成功 --> 返回对方是否收到这个信息 --> 后续处理 即:这个意图包装了短信是否被对方收到的状态信息(供应商已经发送成功,但是对方没有收到)。如果不为null,当短信发送成功或失败就广播,异常:如果destinationAddress或data是空时,抛出IllegalArgumentException异常。。
5、处理返回的发送状态
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent,
0);
// register the Broadcast Receivers
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(context, "短信发送失败", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
}
}
}, new IntentFilter(SENT_SMS_ACTION));
6、处理返回的接收状态
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
// create the deilverIntent parameter
Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0,
deliverIntent, 0);
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
Toast.makeText(context, "收信人已经成功接收", Toast.LENGTH_SHORT).show();
}
}, new IntentFilter(DELIVERED_SMS_ACTION));