Message verification and integration of Mob SMSSDK

There are always SMS verification operations in the process of opening related messages. This week, I didn't have time to write anything new. I would like to share a note about learning SMS verification before. This article uses the SMSSDK provided by Mob

Download SMSSDK

Download address on official website: SMSSDK

Integrated SMSSDK

Put MobCommons.jar, MobTools.jar, SMSSDK-2.0.1.aar and SMSSDKGUI-2.0.1.aar into the libs directory of app. If you do not need SMSSDK with interface, you can not add SMSSDKGUI-2.0.1.aar. For specific files, please refer to the latest SMSSDK.

Configure the build.gradle file

Open the build.gradle file under the app to configure as follows:

Configure Android manifest.xml

Configure permissions and Application in the Android manifest.xml file

Configuration authority
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Configure application
<activity
    android:name="com.mob.tools.MobUIShell"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="stateHidden|adjustResize"/>

Start SDK

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        SMSSDK.initSDK(this, "Your appkey", "Your appsecret");
    }
}

Reference code

Implement a simple case, get the verification code, and verify.

Layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.manu.sharesdksms.MainActivity">
    <LinearLayout
        android:id="@+id/ll_user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:hint="Cell-phone number:" />
        <EditText
            android:id="@+id/et_number"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"/>
        <TextView
            android:id="@+id/tv_getCheckCode"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center"
            android:text="Get verification code"
            android:clickable="true"/>

    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll_pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll_user"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Verification Code:"/>
        <EditText
            android:id="@+id/et_checkCode"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"/>
        <TextView
            android:id="@+id/tv_sendCheckCode"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center"
            android:text="Verification"
            android:clickable="true"/>
    </LinearLayout>
</RelativeLayout>
MainActivity

/**
 * ShareSDk Verification code test
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText et_number;
    private EditText et_checkCode;

    private TextView tv_getCheckCode;
    private TextView tv_sendCheckCode;

    private String phoneNumber;
    private String checkCode;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_number = (EditText) findViewById(R.id.et_number);
        et_checkCode = (EditText) findViewById(R.id.et_checkCode);
        tv_getCheckCode = (TextView) findViewById(R.id.tv_getCheckCode);
        tv_sendCheckCode = (TextView) findViewById(R.id.tv_sendCheckCode);

        checkCode = et_checkCode.getText().toString().trim();

        tv_getCheckCode.setOnClickListener(this);
        tv_sendCheckCode.setOnClickListener(this);

        //Register SMS callback
        SMSSDK.registerEventHandler(ev);
    }

    /**
     * Callback monitoring of SMS verification
     */
    private EventHandler ev = new EventHandler() {
        @Override
        public void afterEvent(int event, int result, Object data) {
            if (result == SMSSDK.RESULT_COMPLETE) { //Callback completed
                //If the verification code is submitted successfully, the data will be returned in the data. Data data type is HashMap < number, code >
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    Log.e("TAG", "Verification code submitted successfully" + data.toString());
                    HashMap<String, Object> mData = (HashMap<String, Object>) data;
                    String country = (String) mData.get("country");//Country number returned
                    String phone = (String) mData.get("phone");//Return the mobile number registered by the user

                    Log.e("TAG", country + "====" + phone);

                    if (phone.equals(phoneNumber)) {
                        runOnUiThread(new Runnable() {//The operation to change the ui should be placed in the main thread. In fact, the handler can be sent
                            @Override
                            public void run() {
                                showDailog("Congratulations! Through validation");
                                dialog.dismiss();
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                showDailog("Validation failed");
                                dialog.dismiss();
                            }
                        });
                    }

                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {//Get the verification code successfully
                    Log.e("TAG", "Get the verification code successfully");
                } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {//Returns the list of countries that support sending verification codes

                }
            } else {
                ((Throwable) data).printStackTrace();
            }
        }
    };

    private void showDailog(String text) {
        new AlertDialog.Builder(this)
                .setTitle(text)
                .setPositiveButton("Determine", null)
                .show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_getCheckCode:
                toast("getCode");
                getCheckCode();
                break;
            case R.id.tv_sendCheckCode:
                toast("sendCode");
                sendCheckCode();
                break;
        }
    }

    /**
     * Get verification code
     */
    public void getCheckCode() {
        phoneNumber = et_number.getText().toString().trim();
        //Send SMS, transfer in country number and phone number
        if (TextUtils.isEmpty(phoneNumber)) {
            toast("Number cannot be empty!");
        } else {
            SMSSDK.getVerificationCode("+86", phoneNumber);
            toast("Send successfully!");
        }
    }

    /**
     * Submit the verification code to the server and listen for verification in the listening callback
     */
    private void sendCheckCode() {
        checkCode = et_checkCode.getText().toString();
        if (!TextUtils.isEmpty(checkCode)) {
            dialog = ProgressDialog.show(this, null, "Verifying...", false, true);
            //Submit SMS verification code
            SMSSDK.submitVerificationCode("+86", phoneNumber, checkCode);//Country number, mobile number, verification code
            Toast.makeText(this, "Submitted registration information:" + phoneNumber, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Verification code cannot be empty", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Toast
     * @param info
     */
    public void toast(String info){
        Toast.makeText(MainActivity.this, info, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        SMSSDK.unregisterEventHandler(ev);
        super.onDestroy();
    }
}

Testing effect

Wait a moment, gif animation time is a little long, in order to receive messages Oh!

You can choose to pay attention to WeChat's public address: jzman-blog get the latest updates, and exchange learning together!

Tags: Android xml Gradle Mobile

Posted on Wed, 06 Nov 2019 16:21:35 -0500 by sirTemplar