Send SMS verification code using MOB

Now, there is a registration function in the use of various APPs. During registration, we usually send a verification code to the mobile phone, and this function can be quickly realized through the Mob collection.

The following is a brief introduction to how to use this function to send mobile phone verification codes.

Step 1: Add in repositories in gradle of the current project 

maven {

            url "http://mvn.mob.com/android"

        }

Also add in deoendences

 classpath 'com.mob.sdk:MobSDK:+'

Step 2: Add in the gradle of the project

MobSDK {
    appKey "d580ad56b4b5"
    appSecret "7fcae59a62342e7e2759e9e397c82bdd"


    SMSSDK {}


}

Note: A few lines of code should be level with the android directory

The third part, citing the Mob plugin:

apply plugin: 'com.mob.sdk'

At this point Mob has been associated with your project.

But sometimes an exception may be reported at compile time. At this time, you only need to add these two lines of code to the androidmanifest file and try: manifest root directory:

xmlns:tools="http://schemas.android.com/tools" 
application标签下 
tools:replace="android:name"

it can be solved..

Then add the code in the Java code. It should be noted that there are two ways to send the verification code.

The first type: the existing interface interface is completed:

public void sendCode(Context context) {
    RegisterPage page = new RegisterPage();
    page.setRegisterCallback(new EventHandler() {
        public void afterEvent(int event, int result, Object data) {
            if (result == SMSSDK.RESULT_COMPLETE) {
                / / Process successful result
                HashMap<String,Object> phoneMap = (HashMap<String, Object>) data;
                String country = (String) phoneMap.get("country"); // Country code, such as "86"
                String phone = (String) phoneMap.get("phone"); // phone number, such as "13800138000"
                // TODO uses country code and phone number for subsequent operations
            } else{
                // TODO handles error results
            }
        }
    });
    page.show(context);

}

The second: complete with no interface interface:

// Request verification code, where country represents the country code, such as "86"; phone represents the mobile phone number, such as "13800138000"
public void sendCode(String country, String phone) {
    // Register an event callback to process sending verification code The result of the operation
    SMSSDK.registerEventHandler(new EventHandler() {
        public void afterEvent(int event, int result, Object data) {
            if (result == SMSSDK.RESULT_COMPLETE) {
               // TODO processing successfully gets the result of the verification code
               // Please note , at this time only the request to send the verification code is completed, and the verification code SMS will take a few seconds to arrive
            } else{
                // TODO handles the wrong result
            }
        
        }
    });
    // Trigger the operation
    SMSSDK.getVerificationCode(country, phone );
}
 
// Submit the verification code, where the code represents the verification code, such as "1357"
public void submitCode(String country, String phone, String code) {
    // Register an event callback to process the result of submitting verification code operation
    SMSSDK.registerEventHandler(new EventHandler() {
        public void afterEvent(int event, int result, Object data) {
            if (result == SMSSDK.RESULT_COMPLETE) {
                // TODO handles the successful verification result
            } else{
                // TODO handles the wrong result
            }
        
        }
    });
    // trigger operation
    SMSSDK.submitVerificationCode(country, phone, code) ;
}
 
    protected void onDestroy() {
        super.onDestroy();
        //When the callback is used up, log out, otherwise there may be a memory leak
        SMSSDK.unregisterAllEventHandler();

    };

At this point, a simple function of sending verification codes has been implemented. Let's try it out!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324510097&siteId=291194637