I believe that there are a lot of friends in do tripartite login will join the function of SMS verification, recently just been assigned to achieve this demand, I am a novice, specifically to search the information online, currently using more big guy. Cloud communication and MOB’s SMSSDK, by contrast, I finally chose the completely free MOB product to try my hand, the following is my experience in the process of using SDK, I hope to see some advice from the gods, like to give a careful heart. First of all, I searched the official website of Mob (http://www.mob.com/) on the Internet for the first time. As for the use of Mob, it is customary to open the document for introduction. The usage method introduced here is relatively detailed, and I will not repeat it here. Maven is a common integration of Eclipse, the company’s project is as, the later does not consider, the way is really very suitable for beginners, just need to gradle configuration OK, first paste code:

Add the following script to your root module build.gradle:

Buildscript {// Add MobSDK's Maven address repositories {maven {url"http://mvn.mob.com/android"}} dependencies {// Register MobSDK classpath"com.mob.sdk:MobSDK:+"}} Add the MobSDK plugin and extension in build.gradle under app, such as:"com.mob.sdk"// Register SMSSDK information in the MobSDK extension MobSDK {appKey"d580ad56****"
    appSecret "7fcae59a62342e7e2759e9e397**"
    SMSSDK {}

        }

Copy the code

// AppKey and AppSecret are obtained in the background of Mob to create an application, so if you need to use it, first go to the official website to register an account, enter the background to create and configure gradle configuration above, basically integrated SMSSDK, the document provides the code to call, I directly copied and pasted it into my demo and tested it, which went well beyond expectation. However, my only complaint is that the SMS verification interface is too old-fashioned, and the product side must not be able to pass it. (Put away the idea of being lazy), I went to study the document again silently. See there is a guI-free use method (http://wiki.mob.com/sms-android-%E6%97%A0gui%E6%8E%A5%E5%8F%A3%E8%B0%83%E7%94%A8/), they provide the relevant interface, I will not map the details, Note that if it is your own UI, you need to add it in the same place where Gradle configures the SMSSDK

 SMSSDK{
        gui false

    }

Copy the code

Since it was just a demo test, the UI layout was written haphazard, just to satisfy normal testing, which is a little ugly

RegisterEventHandler registers an event receiver with the SMSSDK. SMSSDK allows developers to register any number of receivers, all of which will receive a message when an event is triggered. Here is part of my code:

EventHandler handler = new EventHandler(){
            @Override
            public void afterEvent(int event, int result, Object data) {
                if(result == smssdk.result_complete){// The callback is completeif(event == smssDK.event_submit_verification_code) {// The verification code is submitted successfully runOnUiThread(newRunnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this,"Verification successful",Toast.LENGTH_SHORT).show(); }}); }else if (event == SMSSDK.EVENT_GET_VOICE_VERIFICATION_CODE){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this,"Voice authentication send",Toast.LENGTH_SHORT).show(); }}); }else ifEVENT_GET_VERIFICATION_CODE){// Obtaining the verification code successfully runOnUiThread(newRunnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this,"Verification code has been sent",Toast.LENGTH_SHORT).show(); }}); }else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
                        Log.i("test"."test"); }}else{
                    ((Throwable)data).printStackTrace();
                    Throwable throwable = (Throwable) data;
                    throwable.printStackTrace();
                    Log.i("1234",throwable.toString());
                    try {
                        JSONObject obj = new JSONObject(throwable.getMessage());
                        final String des = obj.optString("detail");
                        if(! TextUtils.isEmpty(des)){ runOnUiThread(newRunnable() {
                                @Override
                                public void run() { Toast.makeText(MainActivity.this,des,Toast.LENGTH_SHORT).show(); }}); } } catch (JSONException e) { e.printStackTrace(); }}}}; SMSSDK.registerEventHandler(handler);Copy the code

This is followed by simple interface calls such as send validation:

  findViewById(R.id.tv_test1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { phone = etVGetcode.getText().toString(); // Get the verification codeif (TextUtils.isEmpty(phone))
                    Toast.makeText(MainActivity.this,"The number cannot be empty.",Toast.LENGTH_SHORT).show();
                Log.i("1234",phone.toString());
                SMSSDK.getVerificationCode("86",phone,null); }}); findViewById(R.id.tv_test_vcode_valiable).setOnClickListener(new View.OnClickListener() {@override public void onClick(View View) {// Submit the verification codeif (TextUtils.isEmpty(phone))
                    Toast.makeText(MainActivity.this,"The number cannot be empty.",Toast.LENGTH_SHORT).show();
                number = etVCode.getText().toString();
                if (TextUtils.isEmpty(number))
                    Toast.makeText(MainActivity.this,"The number cannot be empty.",Toast.LENGTH_SHORT).show();
                Log.i("1234",phone+","+number);
                SMSSDK.submitVerificationCode("86",phone,number); }});Copy the code

The code is actually relatively simple, the test is the most important validation efficiency, I feel very fast, basic sent after two or three seconds received, more importantly is free ah, or worth recommending, said so much to look at the effect:

Add a note about a pit: