Realization of simple SMS verification function

Realization of simple SMS verification function

I believe that many friends will add the function of SMS verification when doing the three-way login. Recently, I was assigned to realize this demand. I am a newbie, and I specially searched for information on the Internet. At present, I use a lot of Dahan. Cloud Communication also There is the smssdk of MOB. In contrast, I finally chose a completely free MOB product to try it out. Here are some of my experiences in using the SDK. I hope to see some suggestions from the gods, and just be careful if you like it. First of all, I went to Baidu and downloaded Mob's official website (http://www.mob.com/) as a habit of opening the document for the first time. The introduction here is more detailed about the usage method, and I will not do it here To repeat, I browsed through the entire document. The introduction said that there are two methods, one is the maven integration of as, and the other is the ordinary integration of eclipse. The company's project is as, and the latter is not considered. The maven integration method is It's really suitable for novices, you just need to configure gradle and it's OK, save worry and effort, first paste the code:

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

   buildscript {
   // MobSDK maven 
    repositories {
        maven {
            url "http://mvn.mob.com/android"
        }
    }
    dependencies {
       // MobSDK
        classpath "com.mob.sdk:MobSDK:+"
    }
} 
 app build.gradle MobSDK 
// 
apply plugin: "com.mob.sdk"
// MobSDK SMSSDK 
        MobSDK {
    appKey "d580ad56****"
    appSecret "7fcae59a62342e7e2759e9e397**"
    SMSSDK {}

        }

 

//The appkey and appsecret here are obtained when the application is created in the mob background, so when you need to use it, go to the official website to register an account, enter the background to create and configure the above gradle configuration, basically the smssdk is integrated, and the call is provided in the document I copied and pasted the code directly into my demo and tested it. It was unexpectedly smooth, but the only dissatisfaction was that the SMS verification interface was too old-fashioned, and the product must not be able to make it through, (put away the lazy mind), silently again I went to study the document and saw a way to use it without gui (http://wiki.mob.com/sms-android-%E6%97%A0gui%E6%8E%A5%E5%8F%A3%E8 %B0%83%E7%94%A8/), they provide related interfaces, I will not post the map for specifics, you can go to the official website document to see, it should be noted that if it is your own ui, it needs to be before Add where gradle configures smssdk

 SMSSDK{
        gui false

    }

 

Because it is just a demo test, the UI layout is written casually, just to satisfy the normal test, which is a bit ugly.

The following is the call to write interface code. The document provides an initial interface. RegisterEventHandler is used to register an event receiver in SMSSDK. SMSSDK allows developers to register any number of receivers, and all receivers will receive messages when the event is triggered. , The following is part of my code:

EventHandler handler = new EventHandler(){
            @Override
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE){

                   //
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                       //
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this," ",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else if (event == SMSSDK.EVENT_GET_VOICE_VERIFICATION_CODE){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this," ",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                    else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                       //
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this," ",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(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this,des,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        SMSSDK.registerEventHandler(handler);

 

The following is a simple interface call such as sending verification:

  findViewById(R.id.tv_test1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phone = etVGetcode.getText().toString();
               //
                if (TextUtils.isEmpty(phone))
                    Toast.makeText(MainActivity.this," ",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) {
               //
                if (TextUtils.isEmpty(phone))
                    Toast.makeText(MainActivity.this," ",Toast.LENGTH_SHORT).show();
                number = etVCode.getText().toString();
                if (TextUtils.isEmpty(number))
                    Toast.makeText(MainActivity.this," ",Toast.LENGTH_SHORT).show();
                Log.i("1234",phone+","+number);
                SMSSDK.submitVerificationCode("86",phone,number);
            }
        });

 

The code is actually relatively simple. I test the most important verification efficiency. I personally feel it is very fast. I received it within two or three seconds after sending it. More importantly, it s free. It s still worth recommending. :

Add a pit:

This is the place where the SMS product settings are added to the mob background. At that time, the handyman accidentally turned on it by mistake, resulting in the failure to receive the SMS. After a long time, I still asked the technical customer service to find out. Brothers, be more cautious. . The writing is relatively rough, I hope it will be helpful to the students in need ha =. =