首页 > 代码库 > sharesdk短信验证码的集成

sharesdk短信验证码的集成

在ShareSDK官网http://mob.com/注册并创建Android应用.申请APP_key,下载SDK等

根据官网开发文档导入SDK,目录结构如下

技术分享

将以上文件按需放入Android Studio项目所要使用SMSSDK的Module所在的Libs里面,再在Module的build.gradle里面将libs加入仓库(repositories),
然后添加依赖(dependencies )示例如下:

repositories{
    flatDir{
        dirs ‘libs‘ //就是你放aar的目录地址
    }
}
dependencies {
    ....//你的其他依赖
    compile name:‘SMSSDK-<version>‘,ext:‘aar‘
    compile name:‘SMSSDKGUI-<version>‘,ext:‘aar‘
}

 添加权限:

   <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

 然后在“application”下添加如下activity:

 <activity
            android:name="com.mob.tools.MobUIShell"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:windowSoftInputMode="stateHidden|adjustResize"/>

 代码实现:

初始化工作:

SMSSDK.initSDK(this, "您的appkey", "您的appsecret");

 如果使用默认的短信注册页面可以用以下代码实现:

//打开默认注册页面
RegisterPage registerPage = new RegisterPage();
registerPage.setRegisterCallback(new EventHandler() {
    public void afterEvent(int event, int result, Object data) {
    // 解析注册结果
    if (result == SMSSDK.RESULT_COMPLETE) {
        @SuppressWarnings("unchecked")
        HashMap<String,Object> phoneMap = (HashMap<String, Object>) data;
        String country = (String) phoneMap.get("country");
        String phone = (String) phoneMap.get("phone"); 
        // 提交用户信息(此方法可以不调用)
            registerUser(country, phone);
    }
    }
});
registerPage.show(context);                        

 自定义UI实现手机注册短信验证:

package com.loaderman.smssdkdemo;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class MainActivity extends AppCompatActivity {

    private EditText       et_number;
    private String         number;
    private EditText       et_security;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //SMSSDK.initSDK(this, "您的appkey", "您的appsecret");
        //初始化工作,建议放在自己的Application的onCreate方法中
        SMSSDK.initSDK(this, "1beb22f8862dc", "df387854c513ca138cd23d53cf33cec3");
        et_number = (EditText) findViewById(R.id.et_phone);
        et_security = (EditText) findViewById(R.id.et_security);
        //注册短信回调监听
        SMSSDK.registerEventHandler(ev); 
    }

    /**
     * 短信验证的回调监听
     */
    private EventHandler ev = new EventHandler() {
        @Override
        public void afterEvent(int event, int result, Object data) {
            if (result == SMSSDK.RESULT_COMPLETE) { //回调完成
                //提交验证码成功,如果验证成功会在data里返回数据。data数据类型为HashMap<number,code>
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    Log.e("TAG", "提交验证码成功" + data.toString());
                    HashMap<String, Object> mData = http://www.mamicode.com/(HashMap) data;"country");//返回的国家编号
                    String phone = (String) mData.get("phone");//返回用户注册的手机号

                    Log.e("TAG", country + "====" + phone);

                    if (phone.equals(number)) {
                        runOnUiThread(new Runnable() {//更改ui的操作要放在主线程,实际可以发送hander
                            @Override
                            public void run() {
                                showDailog("恭喜你!通过验证");
                                dialog.dismiss();
                                //    Toast.makeText(MainActivity.this, "通过验证", Toast.LENGTH_SHORT).show();
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                showDailog("验证失败");
                                dialog.dismiss();
                                //     Toast.makeText(MainActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {//获取验证码成功
                    Log.e("TAG", "获取验证码成功");
                } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {//返回支持发送验证码的国家列表

                }
            } else {
                ((Throwable) data).printStackTrace();
            }
        }
    };
     //验证结果弹窗
    private void showDailog(String text) {
        new AlertDialog.Builder(this)
                .setTitle(text)
                .setPositiveButton("确定", null)
                .show();
    }
    /**
     * 获取验证码
     * @param v
     */
    public void getSecurity(View v) {
        number = et_number.getText().toString().trim();
        //发送短信,传入国家号和电话---使用SMSSDK核心类之前一定要在MyApplication中初始化,否侧不能使用
        if (TextUtils.isEmpty(number)) {
            Toast.makeText(this, "号码不能为空!", Toast.LENGTH_SHORT).show();
        } else {
            SMSSDK.getVerificationCode("+86", number);
            Toast.makeText(this, "发送成功:" + number, Toast.LENGTH_SHORT).show();
        }
    }
    /**
     * 向服务器提交验证码,在监听回调中判断是否通过验证
     * @param v
     */
    public void testSecurity(View v) {
        String security = et_security.getText().toString();
        if (!TextUtils.isEmpty(security)) {
            dialog = ProgressDialog.show(this, null, "正在验证...", false, true);
            //提交短信验证码
            SMSSDK.submitVerificationCode("+86", number, security);//国家号,手机号码,验证码
            Toast.makeText(this, "提交了注册信息:" + number, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //要在activity销毁时反注册,否侧会造成内存泄漏问题
        SMSSDK.unregisterAllEventHandler();
    }
}

 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context="com.loaderman.smssdkdemo.MainActivity">

    <EditText
        android:id="@+id/et_phone"
        android:background="#ccc"
        android:hint="请输入手机号码"
        android:inputType="phone"
        android:padding="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <EditText
        android:id="@+id/et_security"
        android:layout_marginTop="10dp"
        android:background="#ccc"
        android:hint="请输入短信验证码"
        android:inputType="phone"
        android:padding="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:onClick="getSecurity"
        android:id="@+id/btn_get"
        android:layout_width="match_parent"
        android:text="获取验证码"
        android:layout_height="wrap_content"/>
     <Button
         android:onClick="testSecurity"
         android:id="@+id/btn_register"
         android:layout_width="match_parent"
         android:text="注册"
         android:layout_height="wrap_content"/>
</LinearLayout>

 效果图:

技术分享

sharesdk短信验证码的集成