关闭

Android基础入门教程——10.2 SmsManager(短信管理器)

标签: android平台android开发frameworkandroidsource code
371人阅读 评论(0) 收藏 举报
分类:

1.调用系统发送短信功能:

就是把写好的收信人和内容发送到系统的发送短信的界面,用户验证收件人内容是否真正确再点击发送! 
说白了就是调用系统发短信的窗口,这样做有一定的好处: 
这样发短信,app安装的时候就可以少写一条发短信的权限,那么诸如360这类安全软件在安装的时候 
就不会提醒用户:”这个APP有短信权限,可能会偷偷滴发短信喔”,而用户对于偷偷发短信的行为是十分 
厌恶的,当然有些人不看直接安装,而有些人可能会觉得会偷偷发短信喔,好恶心的应用,我才不装咧, 
又或者直接禁止我们的APP发送短信,那么当我们APP在发送短信的时候就可能会出现一些异常,或者 
应用直接崩溃等!所以如果你的应用需要发送短信进行验证或者付费这些东西的话,建议使用这种方式!

核心代码

<code class="language-Java hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">SendSMSTo</span>(String phoneNumber,String message){    
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//判断输入的phoneNumber是否为合法电话号码  </span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)){  
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//Uri.parse("smsto") 这里是转换为指定Uri,固定写法  </span>
        Intent intent = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Intent(Intent.ACTION_SENDTO, Uri.parse(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"smsto:"</span>+phoneNumber));        
        intent.putExtra(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sms_body"</span>, message);              
        startActivity(intent);    
    }    
}    </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

2.调用系统提供的短信接口发送短信

这个就需要发短信的权限啦 
<uses-permission android:name=”android.permission.SEND_SMS”/> 
我们直接调用SmsManager为我们提供的短信接口发送短信: 
sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliverIntent); 
参数依次是:

  • destinationAddress:收信人的电话号码
  • scAddress:短信中心的号码,null的话使用当前默认的短信服务中心
  • text:短信内容
  • sentIntent:短信发送状态的信息:(发送状态的Intent) 
    如果不为null,当消息成功发送或失败这个PendingIntent就广播。结果代码是Activity.RESULT_OK 
    表示成功,或RESULT_ERROR_GENERIC_FAILURE、RESULT_ERROR_RADIO_OFF、RESULT_ERROR_NULL_PDU 
    之一表示错误。对应RESULT_ERROR_GENERIC_FAILURE,sentIntent可能包括额外的“错误代码”包含一 
    个无线电广播技术特定的值,通常只在修复故障时有用。每一个基于SMS的应用程序控制检测sentIntent。 
    如果sentIntent是空,调用者将检测所有未知的应用程序,这将导致在检测的时候发送较小数量的SMS。
  • deliverIntent:短信是否被对方收到的状态信息:(接收状态的Intent) 
    如果不为null,当这个短信发送到接收者那里,这个PendtingIntent会被广播, 
    状态报告生成的pdu(指对等层次之间传递的数据单位)会拓展到数据(”pdu”)

…那么复杂,pdu是什么卵?好吧,别纠结,简单知道这些参数是: 
电话号码,信息中心,短信内容,是否发送成功的监听,以及收信人是否接受的监听就好了!

核心代码

<code class="language-Java hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">void</span> sendSMS(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span> phoneNumber,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span> message){  
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取短信管理器   </span>
    android<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>telephony<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>SmsManager smsManager <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> android<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>telephony<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>SmsManager<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>getDefault();  
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//拆分短信内容(手机短信长度限制),貌似长度限制为140个字符,就是</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//只能发送70个汉字,多了要拆分成多条短信发送</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//第四五个参数,如果没有需要监听发送状态与接收状态的话可以写null    </span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">List</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span> divideContents <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> smsManager<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>divideMessage(message);   
    for (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span> text : divideContents) {    
        smsManager<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>sendTextMessage(phoneNumber, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">null</span>, text, sentPI, deliverPI);    
    }  
} </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>

可能你还需要监听短信是否发送成功,或者收信人是否接收到信息,就把下面的加上吧:

1)处理返回发送状态的sentIntent

<code class="language-Java hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//处理返回的发送状态   </span>
String SENT_SMS_ACTION = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"SENT_SMS_ACTION"</span>;  
Intent sentIntent = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Intent(SENT_SMS_ACTION);  
PendingIntent sentPI = PendingIntent.getBroadcast(context, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, sentIntent,  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);  
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//注册发送信息的广播接收者</span>
context.registerReceiver(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> BroadcastReceiver() {  
    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>  
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onReceive</span>(Context _context, Intent _intent) {  
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">switch</span> (getResultCode()) {  
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> Activity.RESULT_OK:
            Toast.makeText(context, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"短信发送成功"</span>, Toast.LENGTH_SHORT).show();  
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>;  
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> SmsManager.RESULT_ERROR_GENERIC_FAILURE:    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//普通错误</span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>;
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> SmsManager.RESULT_ERROR_RADIO_OFF:         <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//无线广播被明确地关闭</span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>;          
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> SmsManager.RESULT_ERROR_NULL_PDU:          <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//没有提供pdu</span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>;      
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> SmsManager.RESULT_ERROR_NO_SERVICE:         <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//服务当前不可用</span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>;              
        }  
    }  
}, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> IntentFilter(SENT_SMS_ACTION));  </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li></ul>

2)处理返回接收状态的deliverIntent

<code class="language-Java hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//处理返回的接收状态   </span>
String DELIVERED_SMS_ACTION = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"DELIVERED_SMS_ACTION"</span>;  
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建接收返回的接收状态的Intent  </span>
Intent deliverIntent = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Intent(DELIVERED_SMS_ACTION);  
PendingIntent deliverPI = PendingIntent.getBroadcast(context, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,deliverIntent, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);  
context.registerReceiver(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> BroadcastReceiver() {  
   <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>  
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onReceive</span>(Context _context, Intent _intent) {  
       Toast.makeText(context,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"收信人已经成功接收"</span>, Toast.LENGTH_SHORT).show();  
   }  
}, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> IntentFilter(DELIVERED_SMS_ACTION)); </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>

另外这里涉及到了广播的知识,如果你对广播不怎么了解的话,可以看下:

Android基础入门教程——4.3.1 BroadcastReceiver牛刀小试 
Android基础入门教程——4.3.2 BroadcastReceiver庖丁解牛


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:103684次
    • 积分:1454
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:131篇
    • 译文:0篇
    • 评论:3条