android_app开发集成mob短信验证码功能

Posted 严少来也

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android_app开发集成mob短信验证码功能相关的知识,希望对你有一定的参考价值。

一.前言

现在的app基本上都需要用到短信功能,注册时或者有消息通知时需要给用户发送一条短信,但是对于个人开发者来说,去买第三方的短信服务实在是有点奢侈,很好的是mob为我们提供了免费的短信验证码服务功能,我不是打广告,的确觉得这项服务很不错。那么下面就简单讲一下如何在自己的工程里集成mob的短信功能,其实整个流程并不复杂,只是个人觉得mob的官方文档有点小乱,官方Demo也有点小复杂,同时有一些细节地方容易被忽视,也会导致一些问题。 
PS:太喜欢mob的logo了。 

二.实现过程

本篇只涉及Android,如果是ios系统,还望自己斟酌,希望本篇文章也能给您提供帮助,同时IDE是android Studio

1.key申请

申请地址:http://www.mob.com,在产品中心选择短信验证码SDK,然后完成相应的注册和申请工作; 
进入自己的后台中心,就可以看见自己的App Key和App Secret: 
 
整体趋势栏给我们展示了一些短信服务使用情况。未上线登记时,我们可以免费使用20条/天,如果需求量比较大,我们可以在自己的工程里集成了mob短信,然后上线登记,应该可以获得更多的免费短信条数,博主暂未尝试。

2.下载SDK

在SDK下载栏目选择SMS for Android,然后选择相应IDE对应的SDK即可(本篇IDE是as) 
 
下载后大概是这样: 

3.集成过程

申请到了key和secret后就是集成到自己的工程中了。mob主要提供两种接口方式:1)使用官方自带的UI;2)使用无GUI接口。

3.1.配置SDK

这已经是使用第三方接口的老规矩了。官网文档我就不贴了,着实看着不舒服,在这里只贴自己的。

  • 首先是在工程的libs下添加jar包和.aar文件。 

  • 然后在build.gradle中添加依赖项 

  • 在AndroidManifest中添加相应权限和注册相应的activity

<code class="language-xml hljs  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-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"><!-- mob短信 需要的权限 --></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.permission.READ_PHONE_STATE"</span> /></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.permission.WRITE_EXTERNAL_STORAGE"</span> /></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.permission.ACCESS_NETWORK_STATE"</span> /></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.permission.ACCESS_WIFI_STATE"</span> /></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.permission.INTERNET"</span> /></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.permission.RECEIVE_SMS"</span> /></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.permission.GET_TASKS"</span> /></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.permission.ACCESS_FINE_LOCATION"</span> /></span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"><!--在application中注册activity --></span>
 <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"><!-- Mob短信(如果使用无GUI的,这个activity应该可以不需要了,自己没试过,就先在这注册着吧) --></span>
        <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">activity
</span>            <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"com.mob.tools.MobUIShell"</span>
            <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:configChanges</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"keyboardHidden|orientation|screenSize"</span>
            <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:theme</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"@android:style/Theme.Translucent.NoTitleBar"</span>
            <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:windowSoftInputMode</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"stateHidden|adjustResize"</span> ></span>
        <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">activity</span>></span></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></ul><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></ul>

3.2.调用接口发送短信

前面提到了,mob短息提供有GUI和无GUI的两种方式,无论哪种方式,都需要对SMSSDK先初始化,再调用接口。

<code class="language-java hljs  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-radius: 0px; word-wrap: normal; background: transparent;">初始化:SMSSDK.initSDK(LoginActivity.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"App Key"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"App Secret"</span>);</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></ul><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></ul>

1)有GUI,即使用mob提供的界面

 
方法如下:

<code class="language-java hljs  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-radius: 0px; word-wrap: normal; background: transparent;"> registerText.setOnClickListener(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> View.OnClickListener() 
            <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;">onClick</span>(View v) 
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//首先初始化SMSSDK</span>
                SMSSDK.initSDK(LoginActivity.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"App Key"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"App Secret"</span>);
                RegisterPage registerPage = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> RegisterPage();
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//回调函数</span>
                registerPage.setRegisterCallback(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> EventHandler()
                
                    <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;">afterEvent</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> event, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> result, Object data)
                    
                    <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;">if</span> (result == SMSSDK.RESULT_COMPLETE)
                    
                        <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;">if</span> (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE)
                        
                        
                        <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;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (event == SMSSDK.EVENT_GET_VERIFICATION_CODE)
                        
                        
                     
                 
                );
                registerPage.show(LoginActivity.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>);
            
        );</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><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li></ul><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><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li></ul>

2)无GUI

这种情况一般是使用自己的activity界面,然后集成短信功能,比如一个简单的注册。

<code class="language-java hljs  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-radius: 0px; word-wrap: normal; background: transparent;"><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;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">ActivityMessageRegister</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extends</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Activity</span> 
</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> CODE_ING = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;                  <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;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> CODE_REPEAT = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>;               <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;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> SMSDDK_HANDLER = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>;            <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;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> TIME = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//倒计时60s</span>

 <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;">protected</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;">onCreate</span>(Bundle savedInstanceState) 
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_register);
        initView();<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//界面初始化</span>
        initSDK();<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//短信初始化</span>
    
    <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;">void</span> initView()
    
        userPhoneText = (EditText)findViewById(R.id.user_phone_input);
        userPasswordText = (EditText)findViewById(R.id.user_password_input);
        userNameText = (EditText)findViewById(R.id.user_name_input);
        registerButton = (Button)findViewById(R.id.register_button);
        registerButton.setOnClickListener(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> OnClickListener());
        loginView = (TextView)findViewById(R.id.login_view);
        getCodeButton = (Button)findViewById(R.id.get_code_button);
        getCodeButton.setOnClickListener(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> OnClickListener());
        codeText = (EditText)findViewById(R.id.code_view);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//</span>
        ...
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//</span>
    
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化SMSSDK</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</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;">initSDK</span>()
    
        SMSSDK.initSDK(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"App Key"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"App Secret"</span>);
        eventHandler = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> EventHandler() 
            <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;">afterEvent</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> event, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> result, Object data) 
                Message msg = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Message();
                msg.arg1 = event;
                msg.arg2 = result;
                msg.obj = data;
                msg.what = SMSDDK_HANDLER;
                handler.sendMessage(msg);
            
        ;
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 注册回调监听接口</span>
        SMSSDK.registerEventHandler(eventHandler);
    
    <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;">private</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">OnClickListener</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">implements</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">View</span>.<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">OnClickListener</span>
    </span>
        <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;">onClick</span>(View v) 
            userPhone = userPhoneText.getText().toString();
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">switch</span> (v.getId()) 
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> R.id.get_code_button:<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;">new</span> AlertDialog.Builder(ActivityMessageRegister.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>)
                            .setTitle(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"发送短信"</span>)
                            .setMessage(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"我们将把验证码发送到以下号码:\\n"</span>+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"+86:"</span>+userPhone)
                            .setPositiveButton(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"确定"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> DialogInterface.OnClickListener()
                            
                                <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;">onClick</span>(DialogInterface dialog, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> which)
                                
                                    SMSSDK.getVerificationCode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"86"</span>, userPhone);
                                    getCodeButton.setClickable(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>);
                                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Thread(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Runnable()
                                    
                                        <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;">run</span>()
                                        
                                            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>; i > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i--)
                                            
                                                handler.sendEmptyMessage(CODE_ING);
                                                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (i <= <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</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;">try</span>
                                                
                                                    Thread.sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>);
                                                 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (InterruptedException e)
                                                
                                                    e.printStackTrace();
                                                
                                            
                                            handler.sendEmptyMessage(CODE_REPEAT);
                                        
                                    ).start();
                                
                            )
                            .create()
                            .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> R.id.register_button:<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//注册</span>
                    SMSSDK.submitVerificationCode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"86"</span>, userPhone, codeText.getText().toString());<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;">default</span>:
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>;
            
        
    
    Handler handler = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Handler()
    
        <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;">handleMessage</span>(Message msg)
        
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">switch</span> (msg.what)
            
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> CODE_ING:<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//已发送,倒计时</span>
                    getCodeButton.setText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"重新发送("</span>+--TIME+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"s)"</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> CODE_REPEAT:<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//重新发送</span>
                    getCodeButton.setText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"获取验证码"</span>);
                    getCodeButton.setClickable(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</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> SMSDDK_HANDLER:
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> event = msg.arg1;
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> result = msg.arg2;
                    Object data = msg.obj;
                    <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;">if</span> (result == SMSSDK.RESULT_COMPLETE)
                    
                        <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;">if</span> (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE)
                        
                            Toast.makeText(ActivityMessageRegister.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"验证成功"</span>, Toast.LENGTH_LONG).show();
                            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (check())<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//其他合法性的检测</span>
                            
                                UserModel user  = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> UserModel();
                                user.setUserId(MyUUID.getUUID());              <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//id</span>
                                user.setUserPhone(userPhone);
                                user.setUserPassword(MD5.md5(userPassword));   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//md5加密</span>
                                user.setUserGender(gender);                    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//性别</span>
                                user.setUserName(userName);
                                user.setUserBirthday(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"19920401"</span>);                      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//暂时为空</span>
                                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//user.setUserIdCard(userIdCard);</span>
                                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//user.setUserImage("");                         //暂时为空</span>
                                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//注册->服务器</span>
                                UserController.userRegister(user, handler);
                            

                        
                        <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;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (event == SMSSDK.EVENT_GET_VERIFICATION_CODE)
                        
                            Toast.makeText(getApplicationContext(), <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;">else</span>
                        
                            ((Throwable) data).printStackTrace();
                        
                    
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(result==SMSSDK.RESULT_ERROR)
                    
                        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> 
                            Throwable throwable = (Throwable) data;
                            throwable.printStackTrace();
                            JSONObject object = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> JSONObject(throwable.getMessage());
                            String des = object.optString(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"detail"</span>);<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;">int</span> status = object.optInt(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"status"</span>);<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;">if</span> (status > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> && !TextUtils.isEmpty(des)) 
                                Toast.makeText(getApplicationContext(), des, Toast.LENGTH_SHORT).show();
                                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
                            
                         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (Exception e) 
                            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//do something</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> R.id.register_status:
                    String result_code = msg.getData().getString(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"result"</span>).toString();
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1"</span>.equals(result_code))
                    
                        Toast.makeText(ActivityMessageRegister.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"注册成功"</span>, Toast.LENGTH_LONG).show();
                        Intent intent = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Intent(ActivityMessageRegister.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>,LoginActivity.class);
                        intent.putExtra(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"userPhone"</span>, userPhone);
                        ActivityMessageRegister.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.setResult(RESULE_CODE, intent);
                        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//startActivity(intent);</span>
                        finish();
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>
                    
                        Toast.makeText(ActivityMessageRegister.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"注册失败"</span>, Toast.LENGTH_LONG).show();
                    
                    <span class="hljs-key

以上是关于android_app开发集成mob短信验证码功能的主要内容,如果未能解决你的问题,请参考以下文章

android_app开发集成mob短信验证码功能

Android开发:app工程集成mob短信验证码功能

Android开发 申请Mob的SMSSDK的短信验证码功能中获取MD5签名(更新中)

sharesdk短信验证码的集成

Android开发之MOB短信验证SDK的使用

mob免费短信验证码安卓SDK调用方法