http://ziyu-1.iteye.com/blog/1013932
android中可以通过两种方式发送短信
第一:调用系统短信接口直接发送短信;主要代码如下:
-
- SmsManager smsManager = SmsManager.getDefault();
- List<String> divideContents = smsManager.divideMessage(content);
- for (String text : divideContents) {
- smsManager.sendTextMessage("150xxxxxxxx", null, text, sentPI, deliverPI);
- }
//直接调用短信接口发短信
SmsManager smsManager = SmsManager.getDefault();
List<String> divideContents = smsManager.divideMessage(content);
for (String text : divideContents) {
smsManager.sendTextMessage("150xxxxxxxx", null, text, sentPI, deliverPI);
}
第二:调起系统发短信功能;主要代码如下:
- Uri uri = Uri.parse("smsto:10010");
- Intent it = new Intent(Intent.ACTION_SENDTO, uri);
- it.putExtra("sms_body", "102");
- activity.startActivity(it);
Uri uri = Uri.parse("smsto:10010");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "102");
activity.startActivity(it);
这里主要讲解第一种方法,其中大部分信息来源于互联网:
1.获取短信管理器
- SmsManager smsManager = SmsManager.getDefault();
SmsManager smsManager = SmsManager.getDefault();
2.拆分短信内容(手机短信长度限制)
- List<String> divideContents = smsManager.divideMessage(content);
List<String> divideContents = smsManager.divideMessage(content);
3.发送拆分后的内容
- List<String> divideContents = smsManager.divideMessage(content);
- for (String text : divideContents) {
- smsManager.sendTextMessage("150xxxxxxxx", null, text, sentPI, deliverPI);
- }
List<String> divideContents = smsManager.divideMessage(content);
for (String text : divideContents) {
smsManager.sendTextMessage("150xxxxxxxx", null, text, sentPI, deliverPI);
}
4.处理返回的发送状态
- String SENT_SMS_ACTION = "SENT_SMS_ACTION";
- Intent sentIntent = new Intent(SENT_SMS_ACTION);
- PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent,
- 0);
-
- 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:
- break;
- case SmsManager.RESULT_ERROR_RADIO_OFF:
- break;
- case SmsManager.RESULT_ERROR_NULL_PDU:
- break;
- }
- }
- }, new IntentFilter(SENT_SMS_ACTION));
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:
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
}
}
}, new IntentFilter(SENT_SMS_ACTION));
5.处理返回的接收状态
- String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
-
- 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));
发送短信的参数说明
- smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
-- destinationAddress:目标电话号码
-- scAddress:短信中心号码,测试可以不填
-- text: 短信内容
-- sentIntent:发送 -->中国移动 --> 中国移动发送失败 --> 返回发送成功或失败信号 --> 后续处理 即,这个意图包装了短信发送状态的信息
-- deliveryIntent: 发送 -->中国移动 --> 中国移动发送成功 --> 返回对方是否收到这个信息 --> 后续处理 即:这个意图包装了短信是否被对方收到的状态信息(供应商已经发送成功,但是对方没有收到)。