As a developer, the login function is indispensable for the development of any App (with the exception of a few special cases). There are generally only three traditional ones: account (email) password, SMS verification and scanning code login. However, these three methods are too cumbersome, if you forget your password, you may need to send a verification code to your encrypted email or mobile phone, fill in the password again, and then log in, this process is very troublesome. Not only as a developer, but also as a user (there are some apps that use face unlock, fingerprint, gesture unlock, etc., but it’s less common). Later, the Internet access information, mobile, telecom, Unicom have their own one-key login SDK, at the same time there are some other one-key login SDK appeared on the market. Since I am a long-time user of SharesDK, I accidentally found a Mob second test on their official website, so I tried it. The following is a detailed description of my entire use process.

I. Developer account and management center

(a) Open the official website to register an account, and then log in. Click on the top rightProduct center.

Note: In order to better use the product, please fill in the real name authentication information.

(2) ClickDeveloper platformAnd into theManagement console.

(3) Create an application, and the name of the application is recommended to be the same as the name of the application you develop.

(4) Improve the application information to facilitate the examination and approval.

(5) Add the second check function module

Click + on the right of the seconds and select Ok to add in the popover.

Then click OK.

(vi) Submission for review

Click second test, click Immediate Audit.

Then enter the application type, industry, application introduction, platform, package name, and signature.

There are three ways to obtain the signature MD5 string:

Method 1: Again, if you don’t know what your App’s signature is, you can use this utility class. (After the project creates the signature, the signature is obtained, and then the application audit fills in the signature information.)

/** * Public class PackageUtils {private static final char[] hexDigits = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102}; private static final String TAG ="PackageUtils".getClass().getSimpleName(); /** * gets the package name of the current application ** @param context context object * @returnReturn package name */ public String getAppPackageName(Context Context) {// Current application pid int pid = Android.os.process.mypId (); ActivityManager = (ActivityManager) context.getSystemService(context.activity_service); / / to iterate through all the application List < ActivityManager. RunningAppProcessInfo > infos = manager. GetRunningAppProcesses ();for (ActivityManager.RunningAppProcessInfo info : infos) {
            if (info.pid == pid) {
                returninfo.processName; }}return ""; } public String getAppSignature(Activity Activity) {PackageManager Manager = activity.getPackageManager(); PackageInfo PackageInfo = null; Try {// The package name passed in is packageInfo = manager.getPackageInfo(getAppPackageName(Activity), packagemanager.get_signatures); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } /******* get Signature array *******/ Signature[] signatures = packageinfo.signatures; String result = hexdigest(signatures[0].toByteArray());if(result ! = null) { Log.d(TAG,"Signature:" + result);
            return result;
        } else {
            Log.d(TAG, "No signature obtained. Please try again.");
        }
        return null;
    }

    public String hexdigest(byte[] paramArrayOfByte) {
        try {
            MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
            localMessageDigest.update(paramArrayOfByte);
            byte[] arrayOfByte = localMessageDigest.digest();
            char[] arrayOfChar = new char[32];
            int i = 0;
            int j = 0;
            while (true) {
                if (i >= 16) {
                    return new String(arrayOfChar);
                }
                int k = arrayOfByte[i];
                int m = j + 1;
                arrayOfChar[j] = hexDigits[(0xF & k >>> 4)];
                j = m + 1;
                arrayOfChar[m] = hexDigits[(k & 0xF)];
                i++;
            }
        } catch (Exception localException) {
        }
        returnnull; }}Copy the code

Then add two lines of code to your first Activity’s onCreate() or Application’s onCreate() method, which can be viewed in the log.

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // Add two lines of code String appSignature = new PackageUtils().getAppSignature(mainActivity.this); Log.d("MainActivity ".The 32-bit signature is:+ appSignature); }}Copy the code

Method 2: If you do not want trouble, you can directly use the command line.

  • Prerequisites: 1. Add the JDK directory keytool.exe to the environment variable. 2. Generate a signature file. The last parameter in this command requires the path for saving the signature file.
 keytool -list -v -keystore C:\Users\Administrator\Desktop\data.jks
Copy the code

Option 3: Click signingReport in Gradle Task

(7) Under review

Once the review is complete, you can have fun developing.


Ii. Install the SDK package and use it

Root gradle file:

Module gradle file:

Download the SDK package:

An error has occurred:

Solve a problem:

Now that we’ve configured these steps, it’s time to write the code.


Third, core code analysis

To initialize the SDK

Declare initialization in the onCreate method in our Application file:

MobSDK.init(this);
Copy the code

Or add the attribute android:name=” com.mob.mobApplication “directly to the appliaction node in manifest.xml.

Pre-login: Invoke the pre-login interface in advance to speed up the non-secret login process and improve user experience

SecVerify.preVerify(new OperationCallback() {
			@Override
			public void onComplete(Object data) {
				// Nothing to do
			}

			@Override
			public void onFailure(VerifyException e) {
				// Nothing to do}});Copy the code

Secret free login:

SecVerify.verify(new MyVerifyCallback()); Class MyVerifyCallback extends VerifyCallback{@override public voidonOtherLogin() {/ / users to click on "other means", deal with their own logic CommonProgressDialog. DismissProgressDialog (); Toast.makeText(MainActivity.this,"Log in with another account", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onComplete(VerifyResult data) {
            CommonProgressDialog.dismissProgressDialog();
            if(data ! = null) { Log.d(TAG, data.toJSONString()); / / to get authorization code is successful, the token information to the application server, by the application server to log in again to verify, which functions should be implemented by the developers themselves CommonProgressDialog. ShowProgressDialog (MainActivity. This); Logintask.getinstance ().login(data, new ResultListener<LoginResult>() { @Override public void onComplete(LoginResult data) { CommonProgressDialog.dismissProgressDialog(); Log.d(TAG,"Login success. data: "+ data.toJSONString()); vibrate(); GotoSuccessActivity (data) page is displayed. } @override public void onFailure(DemoException e) {log. e(TAG,"login failed", e); CommonProgressDialog.dismissProgressDialog(); // error code int errCode = LLDB etCode(); // Error message String errMsg = LLDB etMessage(); T = LLDB (); t = LLDB (); t = LLDB (); String errDetail = null;if(t ! = null) { errDetail = t.getMessage(); } String msg ="Obtaining authorization code succeeded, application server login failed" + "\n Error code:" + errCode + "\n Error message:" + errMsg;
                        if(! TextUtils.isEmpty(errDetail)) { msg +="\n Details:" + errDetail;
                        }
                        if(! devMode) { msg ="Current network exception"; } Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); }}); }} @ Override public void onFailure (VerifyException e) {/ / login failed CommonProgressDialog. DismissProgressDialog (); Log.e(TAG,"verify failed", e); // error code int errCode = LLDB etCode(); // Error message String errMsg = LLDB etMessage(); T = LLDB (); t = LLDB (); t = LLDB (); String errDetail = null;if(t ! = null) { errDetail = t.getMessage(); } String msg =Error code: + errCode + "\n Error message:" + errMsg;
            if(! TextUtils.isEmpty(errDetail)) { msg +="\n Details:" + errDetail;
            }
            if(! devMode) { msg ="Current network exception";
                if (errCode == VerifyErr.C_ONE_KEY_USER_CANCEL_GRANT.getCode()
                        || errCode == VerifyErr.C_LACK_OF_PERMISSIONS.getCode()
                        || errCode == VerifyErr.C_NO_SIM.getCode()
                        || errCode == VerifyErr.C_UNSUPPORTED_OPERATOR.getCode()) {
                    msg = errMsg;
                }
            }
            Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    }



  private void gotoSuccessActivity(LoginResult data) {
        Intent i = new Intent(this, SuccessActivity.class);
        i.putExtra(Const.EXTRAS_DEMO_LOGIN_RESULT, data);
        startActivityForResult(i, REQUEST_CODE);
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
        if(vibrator ! = null) {if (Build.VERSION.SDK_INT >= 26) {
                VibrationEffect vibrationEffect = VibrationEffect.createOneShot(500, 20);
                vibrator.vibrate(vibrationEffect);
            } else{ vibrator.vibrate(500); }}}Copy the code

Customize the authorization page (this is not the point, so I won’t expand too much)

For more details, please check the official documentation: wiki.mob.com/secverify Integration…


4. Demo illustration

One-click authentication

Loading progress prompt:

Click login (default is to agree to the agreement)

Authentication is successful

Tip: The first verification is slow, about 5 seconds. After the first successful verification, you can enter the App again and verify login in less than 1 second.


Five, notes:

(a) must open the flow, second check is through the mobile phone network to determine the phone number. Remember to turn on the flow.

(two) don’t forget to add permission.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 
<uses-permission android:name="android.permission.GET_TASKS" />
Copy the code

(iii) If it is a dual-sim phone, please make sure that you use the same SIM card for dialing and Internet access, otherwise there will be an error. (Two cards of my mobile phone are respectively: one mobile card and one Unicom SIM card)

See the screenshot below:

1. Dial mobile and connect to the Internet.

  • Set up dial-up mobile, Internet Unicom:

  • Example runtime error diagram:

2. Dial-up Unicom, Internet mobile.

  • Set up dial-up unicom, Internet access and mobile:

  • Example runtime error diagram:


Six, the general process of non-secret login

The three major operators in China have issued corresponding SDK for secret free login. I searched the SDK documents of one-click login of China Mobile, China Telecom and China Unicom, and found that the document of China Mobile was the most complete, while that of China Telecom was not. The official website of China Unicom could not be opened.

Specific documents are linked as follows:

  • Open platform for mobile-Internet capabilities
  • Telecom – Tianyi account open platform
  • Unicom – WO+ open platform http://open.wo.com.cn? Can’t open? What’s the matter?

The system interaction flow of one-click login is similar, which is mainly divided into four steps:

  • First, the number authentication SDK is initialized.
  • Step 2, invoke the authorization page.
  • Step 3, agree to authorize and log in.
  • The fourth step, initiate the number.

The details are shown in the figure below:


Seven, advantages and disadvantages:

Advantages:

  • 1. After entering a mobile phone number or obtaining user authorization, the user can complete authentication with one key, which is convenient and quick, saves time and effort, and reduces the time consuming of login. It takes about a second to log in. It’s so convenient.
  • 2. Reduce SMS and email trouble when you forget your account password. You can log in as long as you have your mobile phone number. It is much more convenient than ordinary mobile phone SMS verification.
  • 3. Support whole-network mobile phone number authentication of China’s three major operators, one-point access and nationwide network coverage.
  • 4. It applies to the scenarios of registration, login, and security risk control based on the mobile phone number. It can realize the verification without user perception and broaden the verification scenarios.
  • 5. For dual-card mobile phones, you can switch mobile phone number verification with one key, which is also very convenient.

Disadvantages:

  • 1. The SIM card used for calls and networks must be the same. Otherwise, the authentication fails.
  • 2. Ensure that traffic is enabled. Otherwise, authentication fails.
  • 3. Please ensure that the mobile phone card is in normal use.
  • 4. The official document is suggested to be improved. The entrance is a little hard to find. It is also recommended to include the most frequently asked questions and questions that developers are most concerned about.

As for whether or not to charge, and what the charge standard is, it is not my thing to worry about, please refer to the official documents or consult relevant customer service. I’m just a developer, and if mob seconds works so well, of course I’ll support it.


Viii. Download of relevant materials:

  • Download center: http://www.mob.com/download
  • Integrated document: http://wiki.mob.com/secverify/integrated document
  • The sample code: https://github.com/MobClub/SecVerify-for-Android