获取短信验证码并实现登录

来源:互联网 发布:网点纸淘宝 编辑:程序博客网 时间:2024/06/13 12:09

其实短信验证很简单  下面我把过程简单的写一下,希望对大家有帮助



首先我是这样一个界面




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"    android:orientation="vertical"    tools:context="xi.myapplication.MainActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="30dp"        android:background="@null"        android:orientation="horizontal">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@mipmap/ic_launcher" />        <EditText            android:id="@+id/ed"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:hint="请输入您的手机号" />        <Button            android:id="@+id/button"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="40dp"            android:background="#ffff37"            android:text="获取验证码" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="30dp"        android:layout_marginRight="130dp"        android:layout_marginTop="20dp"        android:background="@null"        android:orientation="horizontal">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@mipmap/ic_launcher" />        <EditText            android:id="@+id/ed1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="请输入收到的验证码" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:orientation="horizontal"        android:layout_height="wrap_content">        <Button            android:id="@+id/bts"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="5"            android:text="提交" />    </LinearLayout></LinearLayout>

其次呢,我们需要去mob官网下载SDK:http://www.mob.com/#/

选择android的SDK下载




这里有两个选择,看自己用什么开发平台了,选择自己需要的

这里我讲as的例子:

下载好之后我们返回找到自己登陆的头像选择进入后台


进入之后选这个



进去之后会给你一个APPkey 和  APPsecret   在下面会有这样一个东西


如果你是上线应用的话   需要选择第二个  进去设置一些东西,我这里直接选第一个,里面是一些相应的教程,

首先我以AS为例,把下载好的SDK中的四个jar包导入你的项目中然后打开这个东西


dependencies {
    ....//你的其他依赖
    compile name:'SMSSDK-<version>',ext:'aar'
    compile name:'SMSSDKGUI-<version>',ext:'aar'
}

这个的意思就是把你导入的jar包 


这两个直接依赖


这两个直接把上面的dependencies中的版本<version>改成2.1.1,不要尖括号;


这样的话我们就基本配置好了


然后就是需要一些代码来完成我们所需要的功能

在AndroidManifest.xml中  复制这些代码

<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    android:name="com.mob.tools.MobUIShell"    android:theme="@android:style/Theme.Translucent.NoTitleBar"    android:configChanges="keyboardHidden|orientation|screenSize"    android:windowSoftInputMode="stateHidden|adjustResize"/>
然后就是activity中代码如下:  首先我们进入这个看一下他的代码


  我们相当于是用了接口回调 的原理  我们把获取验证码提交给后台,然后后台会给我们匹配一个验证码发送到手机上,就是相当于在后台给这个手机号有了一个id,就是这个验证码,只有你输入正确,和他给的这个相匹配,才能进入某个界面或是登录一些信息等等。。。


逻辑很简单  下面我直接上代码:

注意:#号改成你自己的key值。

<pre name="code" class="java">public class MainActivity extends AppCompatActivity {    @InjectView(R.id.ed)    EditText ed;    @InjectView(R.id.button)    Button button;    @InjectView(R.id.ed1)    EditText ed1;    @InjectView(R.id.bts)    Button bts;    private String trim;    private String code;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.inject(this);        SMSSDK.initSDK(this, "##########", "#############");    }    public void getregist() {        EventHandler eh=new EventHandler(){            @Override            public void afterEvent(int event, int result, Object data) {                if (result == SMSSDK.RESULT_COMPLETE) {                    //回调完成                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {                        //提交验证码成功                        runOnUiThread(new Runnable() {                            @Override                            public void run() {                                Toast.makeText(getApplication(),                                        "验证码正确,可以订餐啦!", Toast.LENGTH_SHORT).show();                            }                        });                    }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){                        //获取验证码成功                        runOnUiThread(new Runnable() {                            @Override                            public void run() {                                Toast.makeText(getApplication(),                                        "验证码已发送,请等待", Toast.LENGTH_SHORT).show();                            }                        });                    }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){                        //返回支持发送验证码的国家列表                    }                }else{                    runOnUiThread(new Runnable() {                        @Override                        public void run() {                            Toast.makeText(getApplication(),                                    "获取失败,请重新获取验证码", Toast.LENGTH_SHORT).show();                        }                    });                    ((Throwable)data).printStackTrace();                }            }        };        SMSSDK.registerEventHandler(eh); //注册短信回调    }    @OnClick({R.id.button, R.id.bts})    public void onClick(View view) {        switch (view.getId()) {            case R.id.button:                getregist();                trim = ed.getText().toString().trim();                if (TextUtils.isEmpty(trim)){                    Toast.makeText(getApplication(),                            "输入有误,请重新输入",                            Toast.LENGTH_SHORT).show();                }else{                    SMSSDK.getVerificationCode("86",trim);                }                break;            case R.id.bts:                trim = ed.getText().toString().trim();                code = ed1.getText().toString().trim();                if (TextUtils.isEmpty(trim)&&TextUtils.isEmpty(code)){                    Toast.makeText(getApplication(),                            "请检验你输入的验证码", Toast.LENGTH_SHORT).show();                }else{                    SMSSDK.submitVerificationCode("86",trim,code);                }                break;        }    }}




到这里我们就可以实现简单的短信验证了!快去试试吧,写的应该很详细了,看不懂的可以给我留言评论 我会改正。。。












3 1
原创粉丝点击