android 中调用接口发送短信

android中可以通过两种方式发送短信

第一:调用系统短信接口直接发送短信;主要代码如下:

1.//直接调用短信接口发短信  
2.SmsManager smsManager = SmsManager.getDefault();  
3.List<String> divideContents = smsManager.divideMessage(content);    
4.for (String text : divideContents) {    
5.    smsManager.sendTextMessage("150xxxxxxxx", null, text, sentPI, deliverPI);    
6.}  


第二:调起系统发短信功能;主要代码如下:

1.Uri uri = Uri.parse("smsto:10010");            
2.Intent it = new Intent(Intent.ACTION_SENDTO, uri);            
3.it.putExtra("sms_body", "102");            
4.activity.startActivity(it);  

这里主要讲解第一种方法,其中大部分信息来源于互联网

1.获取短信管理器

SmsManager smsManager = SmsManager.getDefault();

 2.拆分短信内容(手机短信长度限制)

List<String> divideContents = smsManager.divideMessage(content);  

 3.发送拆分后的内容

1.List<String> divideContents = smsManager.divideMessage(content);    
2.for (String text : divideContents) {    
3.    smsManager.sendTextMessage("150xxxxxxxx", null, text, sentPI, deliverPI);    
4.}  

 4.处理返回的发送状态

1.String SENT_SMS_ACTION = "SENT_SMS_ACTION";  
2.Intent sentIntent = new Intent(SENT_SMS_ACTION);  
3.PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent,  
4.        0);  
5.// register the Broadcast Receivers  
6.context.registerReceiver(new BroadcastReceiver() {  
7.    @Override  
8.    public void onReceive(Context _context, Intent _intent) {  
9.        switch (getResultCode()) {  
10.        case Activity.RESULT_OK:  
11.            Toast.makeText(context,  
12.        "短信发送成功", Toast.LENGTH_SHORT)  
13.        .show();  
14.        break;  
15.        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:  
16.        break;  
17.        case SmsManager.RESULT_ERROR_RADIO_OFF:  
18.        break;  
19.        case SmsManager.RESULT_ERROR_NULL_PDU:  
20.        break;  
21.        }  
22.    }  
23.}, new IntentFilter(SENT_SMS_ACTION));  

 5.处理返回的接收状态

扫描二维码关注公众号,回复: 698729 查看本文章
1.String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";  
2.// create the deilverIntent parameter  
3.Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);  
4.PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0,  
5.       deliverIntent, 0);  
6.context.registerReceiver(new BroadcastReceiver() {  
7.   @Override  
8.   public void onReceive(Context _context, Intent _intent) {  
9.       Toast.makeText(context,  
10.  "收信人已经成功接收", Toast.LENGTH_SHORT)  
11.  .show();  
12.   }  
13.}, new IntentFilter(DELIVERED_SMS_ACTION));  

发送短信的参数说明

smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent) 

   

-- destinationAddress:目标电话号码
-- scAddress:短信中心号码,测试可以不填
-- text: 短信内容
-- sentIntent:发送 -->中国移动 --> 中国移动发送失败 --> 返回发送成功或失败信号 --> 后续处理 即,这个意图包装了短信发送状态的信息
-- deliveryIntent: 发送 -->中国移动 --> 中国移动发送成功 --> 返回对方是否收到这个信息 --> 后续处理 即:这个意图包装了短信是否被对方收到的状态信息(供应商已经发送成功,但是对方没有收到)。

男装、女装

猜你喜欢

转载自jinqifu.iteye.com/blog/1843067
今日推荐