One-click login

See guo Lin god in detail this push: curious how to achieve a key login? Come in and see?

A simple summary is that you can click the login button to help you complete the login registration, without entering the mobile phone number, obtaining verification code, waiting, filling in verification code, registration, login and a series of tedious functions. Of course, the prerequisite of one-click login is the support of relevant operators. At present, China Mobile, China Unicom and China Telecom all have relevant open platforms. Obviously, the use of third-party SDK will save us a lot of time and energy. Due to price reasons, the second test introduction of MobTeach platform is selected here.

Let’s take a look at the one-click login page:

Such a page is not a self-written XML file, but comes with the SDK, but it gives developers a highly customized interface and the ability to add their own components.

integration

Here the official website has introduced, I will not repeat. Rapid integration

use

It should be noted that the one-click login SDK obtains the number from the operator through the SIM card of the mobile phone, so the mobile network of the mobile phone needs to be turned on when using this function, and it can still be used when enabled with wifi at the same time.

First, in the LoginActivity onCreate() we need to check whether we have permissions:

/** * Check permission */ private voidcheckPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                PackageManager pm = getPackageManager();
                PackageInfo pi = pm.getPackageInfo(getPackageName(), PackageManager.GET_PERMISSIONS);
                ArrayList<String> list = new ArrayList<>();
                for (String p : pi.requestedPermissions) {
                    if (checkSelfPermission(p) != PackageManager.PERMISSION_GRANTED) {
                        list.add(p);
                    }
                }
                if (list.size() > 0) {
                    String[] permissions = list.toArray(new String[list.size()]);
                    if(permissions ! = null) { requestPermissions(permissions, 1); } } } catch (Throwable t) { t.printStackTrace(); }}}Copy the code

Landing: onRequestPermissionsResult ().

@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); mPresenter.preVerify(); } /** * Override public voidpreVerify() {secverify.preverify (new OperationCallback<Void>() {@override public Void onComplete(Void data) {//TODO processed successfully Logger.d("Successful pre-landing."); verify(); } @override public void onFailure(VerifyException e) {Logger."preVerify 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; } Logger.d(msg); }}); }Copy the code

Pre-login is successful, that is, the login is successful:

    @Override
    public void verify() {// Custom UI customizeUi(); SecVerify.verify(newVerifyCallback() {
            @Override
            public void onOtherLogin() {// The user clicks "Other logins" to process his logical logger.d ()"User chooses another login method");
            }

            @Override
            public void onUserCanceled() {// The user clicks "Close button" or "physical back key" to cancel login and process their own logical logger.d ()"User chooses to cancel login"); } @ Override public void the onComplete (VerifyResult data) {/ / success does not automatically shut down after authorization page, need to manually close SecVerify. FinishOAuthPage (); Logger.d(data.getOperator() +"," + data.getOpToken() + ","+ data.getToken()); UserLogin (data.getToken(), data.getopToken (), data.getOperator())) userLogin(data.getToken(), data.getopToken (), data.getOperator()));  } @Override public void onFailure(VerifyException e) { Logger.d("Login failed:"+ e); }}); }Copy the code

The SDK returns VerifyResult after successful login:

public class VerifyResult extends BaseEntity {
    private String opToken;
    private String token;
    private String operator;

    public VerifyResult(String var1, String var2, String var3) {
        this.opToken = var1;
        this.token = var2;
        this.operator = var3;
    }

    public String getOpToken() {
        return this.opToken;
    }

    public void setOpToken(String var1) {
        this.opToken = var1;
    }

    public String getToken() {
        return this.token;
    }

    public void setToken(String var1) {
        this.token = var1;
    }

    public String getOperator() {
        return this.operator;
    }

    public void setOperator(String var1) { this.operator = var1; }}Copy the code

Where the relevant field definition

variable paraphrase
String opToken Operators token
String token Server token
String operator Operator type, [CMCC: China Mobile, CUCC: China Unicom, CTCC: China Telecom]

You need the back end to fetch your number from the Mob server based on these three fields, so I won’t go into that.

Custom UI:

/** * Customize one-click UI login */ private voidcustomizeUi() {UiSettings UiSettings = new UiSettings.Builder()true) // Whether the title bar is hidden. SetNavHidden (trueSetBackgroundClickClose () // Set whether the background is clicked to close the page.falseSetLogoImgId (r.mipmap.ic_launcher_round) // Whether the Logo is hidden. SetLogoHidden (falseSetLogoWidth (r.modi.dp_68) // setLogoHeight(r.modi.dp_68) // The X-axis offset of the logo // .setloGooffsetx (r.modi.dp_150) // The offset of the Y axis of the logo. SetLogoOffsetY (r.modi.dp_129) // The offset of the right X-axis of the logo // .setloGooffSetrightx (r.ditender.dp_0) // Whether the logo is on the right side of the screen. SetLogoAlignParentRight (false) /** Phone number default horizontal center */ // / desensitized phone number font color resource ID.setNumberColorId (r.color.color333333) // Desensitized phone number font size resource ID .setNumberSizeId(R.ditender.sp_19) // X-axis offset of the desensitized phone number //.setNumberOffsetX(R.Ditender.dp_130) // Y-axis offset of the desensitized phone number .setNumberOffsety (r.demen.dp_230) // The right offset of the x axis of the desensitized phone number //.setNumberOffsetrightx (R.demen.dp_0) // Whether the desensitized phone number is on the right side of the screen .setNumberAlignParentRight(false/** SLOGAN default horizontal center */ // /slogan text size. SetSloganTextSize (R.modi.sp_11) //slogan text color SetSloganTextColor (R.c olor. Color999999) / / slogan y offset setSloganOffsetY (R.d imen. Dp_265) / / slogan y offset / / .setsloganoffsetx (r.modi.dp_130) /** The logon button is in the middle by default. SetLoginBtnImgId (r.davidable. Btn_background_fea100_round_45dp) // Indicates the text resource ID of the login button .setLoginBTNTextid (r.string.verify_login) // The font color resource of the login button ID.setLogInBTNTextColorId (r.color.colorwhite) // The font size of the login button .setLoginBTNTextSize (r.ditender.sp_18) // Login button width.setLoginBtnWidth (r.ditender.dp_280) // Login button height .setLoginBtnHeight(r.ditender.dp_45) // The login button is y offset. SetLoginBtnOffsetY (r.Ditender.dp_295) // The login button is on the right side of the screen .setLoginBtnAlignParentRight(false) /** Switch account default horizontal center */ // switch whether the account is displayed, by default. SetSwitchAccHidden (falseSetSwitchAccText (r.string.verify_change_mobile) // Switch the font size of the account resource ID.setSwitchAccTextSize (R.ditender.sp_14) // Change the font color resource ID.setSwitchAccColorId (r.color.color333333) // Change the X offset of the account //.setSwitchAcCoffsetX (r.dian.dp_130) // Change the account Y offset.setSwitchAccoffsety (r.dimodi.dp_365) /** Privacy protocol */ / Whether to hide the checkbox (set this propertytruewhensetCheckboxDefaultState does not take effect). SetCheckboxHidden (true) // Privacy Protocol Font Color Resource ID (the font color of custom privacy protocols is also affected by this value). SetAgreementColorId (r.color.colorfea100) // Sets the start of privacy protocol text .setAgreementTextStart(r.string.verify_agreement_start) // Sets the mobile privacy protocol text .setagreementCMCCText (r.string.verify_agreement_cmcc) // Set the Unicom Privacy protocol text. SetAgreementCuccText (r.string.verify_agreement_cucc) SetAgreementCtccText (r.string.verify_agreement_CTCC) // Set privacy protocol text link 1 .setAgreementTextAnd1(r.string.verify_agreement_and1) // Customize the text resource ID of the privacy protocol .setcusAgreementNameid1 (r.string.verify_agreement_1) // Custom privacy protocol 1 url.setcusAgreementurL1 (appconfig.test_apply_url) SetCusAgreementColor1 (r.color.colorfea100) // Set the privacy protocol text link 2 .setAgreementTextAnd2(r.string.verify_agreement_and2) // User-defined privacy protocol 2 text resource ID .setcusAgreementNameid2 (r.string.verify_agreement_2) // Custom Privacy Protocol 2 url.setcusAgreementurl2 (appconfig.test_apply_url) SetCusAgreementColor2 (r.color.colorFea100) // Set the privacy protocol text. End .setagreementTextend (r.tring.verify_agreement_end) // Whether the privacy protocol is left-aligned and centered by default. SetAgreementGravityLeft (falseOther text color. / / privacy agreement setAgreementBaseTextColorId (R.c olor. Color999999) / / x axis offset privacy agreement, Default 30. SetAgreementOffsetX (r.demen.dp_40) // Privacy protocol x rightMargin right offset, The default 30. SetAgreementOffsetRightX (R.d imen. Dp_40) / / y offset at the bottom of the privacy agreement. SetAgreementOffsetBottomY (R.d imen. Dp_20) / / sets the status bar to clear the status bar, SetImmersiveTheme (true) / / set the status bar text color is black, only come into force in above 6.0. SetImmersiveStatusTextColorBlack (false// Use pan animation.setTranslateanim (true)
                .build();
        SecVerify.setUiSettings(uiSettings);
    }
Copy the code

Here we need to note that the link of the custom privacy protocol can not be empty, otherwise it will not be displayed successfully. Also, the X-axis offset of some space I commented out is in the default when not set, as long as you set the Y-axis offset on the line, which is the official demo hole.

conclusion

Limited ability, the article may be wrong or not rigorous place, welcome criticism and correction, do not like spray.