Android笔记(五十一) 短信验证码集成——mob平台

      官方网站:www.mob.com

      注册帐号,下载SDK,导入SDK就不说了,主要写一下简单集成如何使用,以后忘记了也可以翻着看看。

      详细的可以参考官方文档:

  http://wiki.mob.com/android-%E7%9F%AD%E4%BF%A1sdk%E6%93%8D%E4%BD%9C%E5%9B%9E%E8%B0%83/

  http://wiki.mob.com/sms-android-%E6%97%A0gui%E6%8E%A5%E5%8F%A3%E8%B0%83%E7%94%A8/

  步骤大致可以分为:

看代码:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.lixyz.records"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" />
    <!-- 为SMSDK添加的权限 -->
    <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.GET_TASKS" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="cn.lixyz.records.activity.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="cn.lixyz.records.activity.RegisterActivity" />
        <activity android:name="cn.lixyz.records.activity.IndexActivity" />
        <activity
            android:name="com.mob.tools.MobUIShell"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:windowSoftInputMode="stateHidden|adjustResize" />
    </application>

</manifest>

RegisterActivity.java

package cn.lixyz.records.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.lixyz.records.R;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class RegisterActivity extends Activity implements OnClickListener {

    private Button bt_reg_getauthcode, bt_reg_sumbit;
    private EditText et_reg_phone, et_reg_authcode, et_reg_password, et_reg_againpass;

    // 用于发送message,表示更改按钮上的倒计时
    public static final int CHANGE_TIME = 100;
    // 用户发送message,表示可以重新获取验证码
    public static final int AGAIN_GET_AUTHCODE = 200;

    int i = 30;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();

        SMSSDK.initSDK(this, "xxxxxxxxx", "xxxxxxxxxxxxxxxx");

        EventHandler eventHandler = 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;
                handler.sendMessage(msg);
            }
        };
        // 注册回调监听接口
        SMSSDK.registerEventHandler(eventHandler);

        bt_reg_getauthcode.setOnClickListener(this);
        bt_reg_sumbit.setOnClickListener(this);
    }

    private void initView() {
        bt_reg_getauthcode = (Button) findViewById(R.id.bt_reg_getauthcode);
        bt_reg_sumbit = (Button) findViewById(R.id.bt_reg_sumbit);
        et_reg_phone = (EditText) findViewById(R.id.et_reg_phone);
        et_reg_authcode = (EditText) findViewById(R.id.et_reg_authcode);
        et_reg_password = (EditText) findViewById(R.id.et_reg_password);
        et_reg_againpass = (EditText) findViewById(R.id.et_reg_againpass);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_reg_getauthcode:
            sendAuthCode();
            break;

        case R.id.bt_reg_sumbit:
            registe();
            break;
        }
    }

    // 注册方法
    private void registe() {
        String inputAuthCode = et_reg_authcode.getText().toString();
        String password = et_reg_password.getText().toString();
        String str = et_reg_againpass.getText().toString();
        if (str.equals(password)) {
            SMSSDK.submitVerificationCode("86", et_reg_phone.getText().toString(), inputAuthCode);
        } else {
            Toast.makeText(getApplicationContext(), "您的密码输入不一致", Toast.LENGTH_SHORT).show();
        }
    }

    // 发送验证码
    private void sendAuthCode() {
        // 获取用户输入的手机号
        String phoneNumber = et_reg_phone.getText().toString();
        // 请求获取验证码
        SMSSDK.getVerificationCode("86", phoneNumber);
        // 将获取验证码按钮设置为不可点击
        bt_reg_getauthcode.setClickable(false);
        // bt_reg_getauthcode.setText("重新发送(" + i + ")");
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 循环30秒,用于更新按钮上的倒计时
                for (; i > 0; i--) {
                    handler.sendEmptyMessage(CHANGE_TIME);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                // 30秒完成,可以重新获取验证码
                handler.sendEmptyMessage(AGAIN_GET_AUTHCODE);
            }
        }).start();

    }

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == CHANGE_TIME) {
                bt_reg_getauthcode.setText("重新发送(" + i + ")");
            } else if (msg.what == AGAIN_GET_AUTHCODE) {
                bt_reg_getauthcode.setClickable(true);
                bt_reg_getauthcode.setText("获取验证码");
                i = 30;
            } else {
                int event = msg.arg1;
                int result = msg.arg2;
                Object data = msg.obj;
                Log.d("TTTT", "event=" + event + ",result=" + result);
                // 如果等于-1,表示事件执行成功
                if (result == -1) {
                    if (event == 3) {
                        Toast.makeText(getApplicationContext(), "提交验证码成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                        startActivity(intent);
                    } else if (event == 2) {
                        Toast.makeText(getApplicationContext(), "验证码已经发送", Toast.LENGTH_SHORT).show();
                    }
                } else if (result == 0) {
                    if (event == 3) {
                        Toast.makeText(getApplicationContext(), "验证码错误,请检查", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        };
    };

    protected void onDestroy() {
        SMSSDK.unregisterAllEventHandler();
    };
}

activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/et_reg_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:hint="输入您的手机号" />

        <Button
            android:id="@+id/bt_reg_getauthcode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="获取验证码" />
    </LinearLayout>

    <EditText
        android:id="@+id/et_reg_authcode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入您收到的验证码" />

    <EditText
        android:id="@+id/et_reg_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="请输入您的密码"
        android:password="true" />

    <EditText
        android:id="@+id/et_reg_againpass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="重复您的密码"
        android:password="true" />

    <Button
        android:id="@+id/bt_reg_sumbit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="注     册" />

</LinearLayout>

  

原文地址:https://www.cnblogs.com/xs104/p/4934912.html