第四天 Kotlin外卖APP -短信验证登录

短信验证登录

布局与主要代码实现

布局

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.activity.LoginActivity"
        android:orientation="vertical"
        android:background="#EDEDEB">

    <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp"
                  android:layout_weight="1.2"
                  android:orientation="horizontal"
                  android:background="#3090E6">

        <ImageView android:layout_width="40dp" android:layout_height="40dp"
        android:id="@+id/iv_goback" android:src="@drawable/goback"
        android:layout_alignParentBottom="true" android:layout_marginLeft="20dp"/>

        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="登陆" android:layout_toRightOf="@id/iv_goback" android:id="@+id/iv_text_login"
        android:textColor="#FFF" android:textSize="24sp" android:layout_alignParentBottom="true"
        android:layout_marginBottom="5dp" android:layout_marginLeft="15dp"/>

        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="密码登陆" android:textSize="20sp" android:textColor="#FFF" android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" android:layout_marginBottom="7dp"/>
    </RelativeLayout>

    <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp"
                  android:layout_weight="8.5">

        <EditText android:layout_width="280dp" android:layout_height="wrap_content"
        android:id="@+id/iv_edit_phone" android:layout_marginTop="30dp"
        android:layout_marginLeft="20dp" android:hint="请输入手机号码"/>
        <Button android:layout_width="wrap_content" android:layout_height="45dp"
        android:id="@+id/iv_btn_houqu" android:text="获取验证码" android:textColor="#FFF"
                android:background="#3090E6" android:layout_toRightOf="@id/iv_edit_phone"
                android:layout_marginTop="20dp" android:layout_marginLeft="10dp"
                tools:ignore="OnClick"/>
        <EditText android:layout_width="100dp" android:layout_height="wrap_content"
        android:id="@+id/iv_edit_number" android:hint="验证码" android:layout_below="@id/iv_edit_phone"
        android:layout_marginLeft="20dp" android:layout_marginTop="10dp"/>

        <Button android:layout_width="match_parent" android:layout_height="wrap_content"
        android:id="@+id/iv_btn_login" android:background="#33C937" android:text="登陆"
        android:layout_below="@id/iv_edit_number" android:layout_marginTop="50dp"
        android:textColor="#FFF" android:textSize="20sp"/>

        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:id="@+id/texeView" android:text="第三方登录"
        android:layout_below="@id/iv_btn_login" android:layout_marginTop="100dp"
        android:textSize="20sp" android:layout_centerHorizontal="true"/>

        <LinearLayout android:layout_width="360dp" android:layout_height="wrap_content"
        android:orientation="horizontal" android:layout_below="@id/texeView" android:id="@+id/iv_lin_image"
        android:layout_marginTop="30dp" android:layout_centerHorizontal="true">
            <ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp"
            android:src="@mipmap/weixin" android:id="@+id/iv_people_weixin" />
            <ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp"
                       android:src="@mipmap/qq" android:id="@+id/iv_people_qq"/>
            <ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp"
                       android:src="@mipmap/weibo" android:id="@+id/iv_people_weibo"/>
            <ImageView android:layout_width="0dp" android:layout_weight="1" android:layout_height="50dp"
                       android:src="@mipmap/taobao" android:id="@+id/iv_people_taobao"/>
        </LinearLayout>

        <LinearLayout android:layout_width="360dp" android:layout_height="wrap_content"
        android:orientation="horizontal" android:layout_below="@id/iv_lin_image"
                      android:layout_marginTop="5dp" android:layout_centerHorizontal="true">
            <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"
                      android:text="微信" android:textSize="20sp" android:gravity="center"/>
            <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"
                      android:text="QQ" android:textSize="20sp" android:gravity="center"/>
            <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"
                      android:text="微博" android:textSize="20sp" android:gravity="center"/>
            <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"
                      android:text="淘宝" android:textSize="20sp" android:gravity="center"/>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

主要代码

class LoginActivity : AppCompatActivity() {

    val eventHandler = object : EventHandler(){
        override fun afterEvent(event: Int, result: Int, data: Any?) {
            if (data is Throwable){
                val msg = data.message
                Log.e("####",msg)
//                Toast.makeText(this@LoginActivity,msg,Toast.LENGTH_SHORT).show()
            }else{
                if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                    //处理自己的逻辑
                    Log.e("###","获取验证码")
                }else if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE){
                    //处理自己的逻辑
                    Log.e("###","提交验证码成功")
                    //登录外卖服务器
                    val phone = iv_edit_phone.text.toString().trim()
                    val preferences = getSharedPreferences("phoneNum", Context.MODE_PRIVATE)
                    val edit = preferences.edit()
                    edit.putString("num",phone)
                    edit.commit()
                    finish()
                }
            }
        }
    }

    lateinit var loginActivityPersenter : LoginActivityPresenter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        loginActivityPersenter = LoginActivityPresenter(this)

        //注册监听器
        SMSSDK.registerEventHandler(eventHandler)
        initListener()

    }

    override fun onDestroy() {
        super.onDestroy()
        SMSSDK.unregisterEventHandler(eventHandler)
    }


    private fun initListener() {
        iv_goback.setOnClickListener{finish()}
        iv_btn_houqu.setOnClickListener{
            //获取验证码
            val phone = iv_edit_phone.text.toString().trim()
            //验证手机号码
            if (SMSUtils.judgePhoneNums(this,phone)){
                SMSSDK.getVerificationCode("86",phone)

                //开启倒计时
                iv_btn_houqu.isEnabled = false
                Thread(CutDownTask()).start()
            }
        }

        iv_btn_login.setOnClickListener{
            //提交验证码
            val phone = iv_edit_phone.text.toString().trim()
            val code = iv_edit_number.text.toString().trim()
//            if (SMSUtils.judgePhoneNums(this,phone) && !TextUtils.isEmpty(code)){
                SMSSDK.submitVerificationCode("86",phone,code)
//            }
        }
    }

    companion object {
        val TIME_MINUS = -1
        val TIME_IS_OUT = 0
    }
    val handler = @SuppressLint("HandlerLeak")
    object : Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            when(msg!!.what){
                TIME_MINUS -> iv_btn_houqu.text = "剩余时间($time)秒"
                TIME_IS_OUT -> {
                    iv_btn_houqu.isEnabled = true
                    iv_btn_houqu.text = "点击重发"
                    time = 60
                }
            }
        }
    }

    var time = 60
    inner class CutDownTask : Runnable{
        override fun run() {
            while (time > 0){
                //刷新剩余时间,当前子线程使用handler
                handler.sendEmptyMessage(TIME_MINUS)
                SystemClock.sleep(999)
                time --
            }
            handler.sendEmptyMessage(TIME_IS_OUT)
        }
    }

    fun onLoginSuccess(){
        finish()
        toast("登录成功")
    }
    fun onLonginFailed(){
        toast("登录失败")
    }
}

短信验证配置

1、打开项目根目录的build.gradle,在buildscrip–>dependencies 模块下面添加
在这里插入图片描述
2、在使用SMSSDK模块的build.gradle中,添加MobSDK插件和扩展
在这里插入图片描述
代码调用
1、有GUI

fun sendCode(context: Context) {
    val page = RegisterPage()
    //如果使用我们的ui,没有申请模板编号的情况下需传null
    page.setTempCode(null)
    page.setRegisterCallback(object : EventHandler() {
        fun afterEvent(event: Int, result: Int, data: Any) {
            if (result == SMSSDK.RESULT_COMPLETE) {
                // 处理成功的结果
                val phoneMap = data as HashMap<String, Any>
                // 国家代码,如“86”
                val country = phoneMap["country"] as String 
                // 手机号码,如“13800138000”
                val phone = phoneMap["phone"] as String 
                // TODO 利用国家代码和手机号码进行后续的操作
            } else {
                // TODO 处理错误的结果
            }
        }
    })
    page.show(context)
}

2、无界面调用

EventHandler eh=new EventHandler(){
@Override
    public void afterEvent(int event, int result, Object data) {
        // TODO 此处不可直接处理UI线程,处理后续操作需传到主线程中操作
        Message msg = new Message();
        msg.arg1 = event;
        msg.arg2 = result;
        msg.obj = data;
        mHandler.sendMessage(msg);

    }
};

//注册一个事件回调监听,用于处理SMSSDK接口请求的结果
SMSSDK.registerEventHandler(eh);

3、、// 请求验证码,其中country表示国家代码,如“86”;phone表示手机号码如“13800138000”

SMSSDK.getVerificationCode(country, phone);

4、// 提交验证码,其中的code表示验证码,如“1357”

SMSSDK.submitVerificationCode(country, phone, code);

5、// 使用完EventHandler需注销,否则可能出现内存泄漏

protected void onDestroy() {
    super.onDestroy();
    SMSSDK.unregisterEventHandler(eventHandler);
}

标注:短信验证配置均参考官方开放文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值