Mob手机验证码android studio实现

来源:互联网 发布:淘宝客服介入会怎么样 编辑:程序博客网 时间:2024/05/21 21:01

       最近自己想做个APP玩玩,想加上手机注册功能,经过多次网上查询,决定使用mob的免费验证码。在网上查询了很多关于mob的资料,好像没有关于最近一个版本的使用,自己探索了一段时间,感觉不懂的人会感觉很烦,决定写一个demo帮助一下有需要的同学。(包含SDK自带的GUI和自己设计的GUI)

         界面如下

     

  1.      1.注册一个有效的mob账号

     2.进入mob后台,使用短信验证SDK,然后添加一个应用(可以是空的应用,后面可以继续登记)


      3.下载sdk,短信验证SDK,最近版本(

SMSSDK For Android v3.0.0 (2017-06-12更新)


      4.环境配置,解压缩后的SDK包中的jar包添加和AndroidManifest.xml文件的修改参照  -- 此  处


      5.layout布局文件(acyivity_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:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.demo9.MainActivity"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        android:layout_marginTop="50dp">        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="sdk自带的GUI"            android:textColor="@color/colorAccent"            android:gravity="center"            android:textSize="16dp"/>        <Button            android:id="@+id/button3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="验证码"            android:layout_gravity="center"            android:layout_margin="20dp"/>    </LinearLayout>    <TextView        android:layout_width="match_parent"        android:layout_height="5dp"        android:background="@color/colorPrimary"/>    <LinearLayout        android:layout_marginTop="20dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical">        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="自定义UI"            android:textColor="@color/colorAccent"            android:gravity="center"            android:textSize="16dp"/>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content">            <EditText                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="2"                android:id="@+id/editText"                android:hint="填写手机号码"                />            <Button                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="获取验证码"                android:id="@+id/button"               />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content">            <EditText                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="2"                android:id="@+id/editText2"                android:hint="填写验证码" />            <Button                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="发送验证码"                android:id="@+id/button2" />        </LinearLayout>    </LinearLayout></LinearLayout>

        6.MainActivity.java

package com.example.demo9;import android.content.Context;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import org.json.JSONObject;import cn.smssdk.EventHandler;import cn.smssdk.SMSSDK;import cn.smssdk.gui.RegisterPage;import cn.smssdk.utils.SMSLog;public class MainActivity extends AppCompatActivity {    private Button btn1,btn2,btn3;    private EditText edt1,edt2;    private String phone;    private Context context;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        context = this;        btn1 = (Button)findViewById(R.id.button);        btn2 = (Button)findViewById(R.id.button2);        btn3 = (Button) findViewById(R.id.button3);        edt1 = (EditText)findViewById(R.id.editText);        edt2 = (EditText)findViewById(R.id.editText2);        EventHandler eh=new EventHandler(){            @Override            public void afterEvent(int event, int result, Object data) {                Message msg = new Message();                msg.arg1 = event;                msg.arg2 = result;                msg.obj = data;                mHandler.sendMessage(msg);            }        };        SMSSDK.registerEventHandler(eh);        btn1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //请求获取短信验证码                SMSSDK.getVerificationCode("86", edt1.getText().toString());                phone = edt1.getText().toString();            }        });        btn2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //提交短信验证码                SMSSDK.submitVerificationCode("86",phone,edt2.getText().toString());            }        });        btn3.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                opeRegisterPager();            }        });    }    Handler mHandler = new Handler()    {        public void handleMessage(Message msg) {            // TODO Auto-generated method stub            super.handleMessage(msg);            int event = msg.arg1;            int result = msg.arg2;            Object data = msg.obj;            Log.e("event", "event=" + event);            if (result == SMSSDK.RESULT_COMPLETE) {                System.out.println("--------result"+event);                //短信注册成功后,返回MainActivity,然后提示新好友                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {                    //提交验证码成功                    Toast.makeText(getApplicationContext(), "提交验证码成功", Toast.LENGTH_SHORT).show();                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){                    //已经验证                    Toast.makeText(getApplicationContext(), "验证码已经发送", Toast.LENGTH_SHORT).show();                }            } else {                int status = 0;                try {                    ((Throwable) data).printStackTrace();                    Throwable throwable = (Throwable) data;                    JSONObject object = new JSONObject(throwable.getMessage());                    String des = object.optString("detail");                    status = object.optInt("status");                    if (!TextUtils.isEmpty(des)) {                        Toast.makeText(MainActivity.this, des, Toast.LENGTH_SHORT).show();                        return;                    }                } catch (Exception e) {                    SMSLog.getInstance().w(e);                }            }        }    };    protected void onDestroy() {        super.onDestroy();        SMSSDK.unregisterAllEventHandler();    };    private void opeRegisterPager() {        //打开注册页面,这个页面就是我们在AndroidManifest.xml中添加的节点com.mob.tools.MobUIShell        RegisterPage registerPage = new RegisterPage();        registerPage.setRegisterCallback(new EventHandler() {            public void afterEvent(int event, int result, Object data) {                // 解析注册结果                if (result == SMSSDK.RESULT_COMPLETE) {                }            }        });        registerPage.show(context);    }}


原创粉丝点击