关于android:Android如何实现自定义短信登录丨MobTech

4次阅读

共计 6023 个字符,预计需要花费 16 分钟才能阅读完成。

短信验证码 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);
}

}

运行效果图如下:

正文完
 0