How to customize SMS login for Android

Time:2022-11-24

The SMS verification code SDK provides developers with a universal SMS verification code tool. Developers can use it to embed the SMS verification code SDK in the App, and simply set up SMS verification. The integration is fast and convenient, and it is easy to manage later

Write xml layout to create your own login xml layout, and create a new activity_custom_login.xml file under the res/layout file, as shown below:
How to customize SMS login for Android

Draw your own xml layout file, you can refer to the following code:

<?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>

Write code to create a login CustomLoginActivity class in the project, and implement the function code as follows:

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("Reacquire");
                    //When the timer ends, stop the timer and restore the value
                    count = 60;
                    timer.cancel();
                    get_code_id.setEnabled(true);
                }else{
                    get_code_id.setText(count+"");
                }
                break;
            case 2:
                Toast.makeText(CustomLoginActivity.this,"Get SMS verification code successfully",Toast.LENGTH_LONG).show();
                break;
            case 3:
                Log.i("Codr","Failed to get SMS verification code");
                Toast.makeText(CustomLoginActivity.this,"Failed to get SMS verification code",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 here is a child thread! Do not directly handle the UI thread! Subsequent operations need to be passed to the main thread for operation!
            if (result == SMSSDK.RESULT_COMPLETE) {
                // success callback
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    // Submit SMS and voice verification code successfully
                } 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) {
                    //Obtain the voice verification code successfully
                    Message message = new Message();
                    message.what = 2;
                    handler.sendMessage(message);
                }
            } else if (result == SMSSDK.RESULT_ERROR) {
                // failure callback
                Message message = new Message();
                message.what = 3;
                handler.sendMessage(message);
            } else {
                //Other failure callbacks
                ((Throwable) data).printStackTrace();
            }
        }
    };
    SMSSDK.registerEventHandler(eventHandler); //register SMS callback
}
@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)){
                // countdown
                CountdownStart();
                getVerificationCode("86",phone);
            }else{
                Toast.makeText(this,"Please enter your phone number",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,"Please enter your phone number",Toast.LENGTH_LONG).show();
            }else if (TextUtils.isEmpty(code)){
                Toast.makeText(this,"Please enter the verification code",Toast.LENGTH_LONG).show();
            }else{
                //login logic
            }
            break;
    }
}
/**
 * cn.smssdk.SMSSDK.class
 * request text verification code
 * @param country Country code
 * @param phone phone number
 */
public static void getVerificationCode(String country, String phone){
    //obtain SMS verification code
    SMSSDK.getVerificationCode(country,phone);
}

/**cn.smssdk.SMSSDK.class
 * request text verification code(带模板编号)
 * @param tempCode template number
 * @param country Country code
 * @param phone phone number
 */
public static void getVerificationCode(String tempCode,String country, String phone){
    //obtain SMS verification code
    SMSSDK.getVerificationCode(tempCode,country,phone);
}

// countdown函数
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();
    // After using the EventHandler, you need to log out, otherwise there may be a memory leak
    SMSSDK.unregisterEventHandler(eventHandler);
}

}

The running effect diagram is as follows:
How to customize SMS login for Android