Android如何实现自定义短信登录丨MobTech

发布于:2023-03-12 ⋅ 阅读:(0) ⋅ 点赞:(0)

短信验证码SDK,为开发者提供全球通用的短信验证码工具,开发者可以用其在App植入短信验证码SDK、简单设置即可短信验证,集成快速便捷,且后期易于管理

编写xml布局创建自己的登录xml布局,在res/layout文件下新建activity_custom_login.xml文件,如下图:
图片

绘制自己的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"
    android:paddingTop="50dp"
    android:paddingRight="10dp"
    tools:context="cn.mob.smssdk.demo.smslogin.ui.login.CustomLoginActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:id="@+id/login_phone_textView"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dp"
            android:text="@string/login_phone" />
        <EditText
            android:id="@+id/editTextPhone"
            android:layout_width="0dp"
            android:layout_weight="9"
            android:layout_height="wrap_content"
            android:ems="10"
            android:text="136****6666"
            android:inputType="phone" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:id="@+id/login_code_textView"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="@string/login_code" />

        <EditText
            android:id="@+id/editTextNumber"
            android:layout_width="0dp"
            android:layout_weight="4"
            android:layout_height="wrap_content"
            android:ems="10"
            android:text="123456"
            android:inputType="number" />
        <Button
            android:id="@+id/get_code_id"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:gravity="center"
            android:layout_height="match_parent"
            android:text="@string/get_code"
            >

        </Button>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="50dp"
        >
        <Button
            android:id="@+id/login_id"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:text="@string/login"
            />

    </LinearLayout>
</LinearLayout>

编写代码在项目中创建登录CustomLoginActivity类,实现功能代码如下:

package xx.xx.xx;

import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.os.Message; 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 com.mob.MobSDK;import java.util.Timer; import java.util.TimerTask;import cn.smssdk.EventHandler; import cn.smssdk.SMSSDK; import cn.smssdk.demo.R;public class CustomLoginActivity extends AppCompatActivity implements View.OnClickListener {

EventHandler eventHandler;
EditText editTextPhone,editTextNumber;
Button get_code_id,login_id;
Timer timer;
int count = 60;
@SuppressLint("HandlerLeak")
Handler handler = new Handler(){
    public void handleMessage(Message msg) {
        int tag = msg.what;
        switch (tag){
            case 1:
                int arg = msg.arg1;
                if(arg==1){
                    get_code_id.setText("重新获取");
                    //计时结束停止计时把值恢复
                    count = 60;
                    timer.cancel();
                    get_code_id.setEnabled(true);
                }else{
                    get_code_id.setText(count+"");
                }
                break;
            case 2:
                Toast.makeText(CustomLoginActivity.this,"获取短信验证码成功",Toast.LENGTH_LONG).show();
                break;
            case 3:
                Log.i("Codr","获取短信验证码失败");
                Toast.makeText(CustomLoginActivity.this,"获取短信验证码失败",Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }

    };
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_login);
    MobSDK.submitPolicyGrantResult(true);
    init();
}
private void init(){
    editTextPhone = findViewById(R.id.editTextPhone);
    editTextNumber = findViewById(R.id.editTextNumber);
    get_code_id = findViewById(R.id.get_code_id);
    get_code_id.setOnClickListener(this::onClick);
    login_id = findViewById(R.id.login_id);
    login_id.setOnClickListener(this::onClick);

   

 eventHandler = new EventHandler() {
        @Override
        public void afterEvent(int event, int result, Object data) {
            // TODO 此处为子线程!不可直接处理UI线程!处理后续操作需传到主线程中操作!
            if (result == SMSSDK.RESULT_COMPLETE) {
                //成功回调
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    //提交短信、语音验证码成功
                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    Message message = new Message();
                    message.what = 2;
                    handler.sendMessage(message);
                } else if (event == SMSSDK.EVENT_GET_VOICE_VERIFICATION_CODE) {
                    //获取语音验证码成功
                    Message message = new Message();
                    message.what = 2;
                    handler.sendMessage(message);
                }
            } else if (result == SMSSDK.RESULT_ERROR) {
                //失败回调
                Message message = new Message();
                message.what = 3;
                handler.sendMessage(message);
            } else {
                //其他失败回调
                ((Throwable) data).printStackTrace();
            }
        }
    };
    SMSSDK.registerEventHandler(eventHandler); //注册短信回调
}
@Override
public void onClick(View view) {
    String phone = "";
    String code = "";
    int id = view.getId();
    switch (id){
        case R.id.get_code_id:
            phone = editTextPhone.getText().toString().trim();
            if (!TextUtils.isEmpty(phone)){
                //倒计时
                CountdownStart();
                getVerificationCode("86",phone);
            }else{
                Toast.makeText(this,"请输入手机号码",Toast.LENGTH_LONG).show();
            }
            break;
        case R.id.login_id:
            phone = editTextPhone.getText().toString().trim();
            code = editTextNumber.getText().toString().trim();
            if (TextUtils.isEmpty(phone)){
                Toast.makeText(this,"请输入手机号码",Toast.LENGTH_LONG).show();
            }else if (TextUtils.isEmpty(code)){
                Toast.makeText(this,"请输入验证码",Toast.LENGTH_LONG).show();
            }else{
                //登录逻辑
            }
            break;
    }
}
/**
 * cn.smssdk.SMSSDK.class
 * 请求文本验证码
 * @param country   国家区号
 * @param phone     手机号
 */
public static void getVerificationCode(String country, String phone){
    //获取短信验证码
    SMSSDK.getVerificationCode(country,phone);
}

/**cn.smssdk.SMSSDK.class
 * 请求文本验证码(带模板编号)
 * @param tempCode  模板编号
 * @param country   国家区号
 * @param phone     手机号
 */
public static void getVerificationCode(String tempCode,String country, String phone){
    //获取短信验证码
    SMSSDK.getVerificationCode(tempCode,country,phone);
}

//倒计时函数
private void CountdownStart(){
    get_code_id.setEnabled(false);
    timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            Message message = new Message();
            message.what = 1;
            message.arg1 = count;
            if(count!=0){
                count--;
            }else {
                return;
            }
            handler.sendMessage(message);
        }
    }, 1000,1000);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    // 使用完EventHandler需注销,否则可能出现内存泄漏
    SMSSDK.unregisterEventHandler(eventHandler);
}

}

运行效果图如下:
image.png


网站公告

欢迎关注微信公众号 关注公众号领取新人福利

今日签到

点亮在社区的每一天
去签到