Android One-Click Login Best Practices

Last updated:2024-06-07
Android One-Click Login Best Practices

This document introduces best practices for Android SDK one-click login.

Supported Versions

v3.0.0 and later.

One-Click Login Flow

Initialize SDK -> Obtain carrier prefetch number -> Set authorization page UI style -> Open authorization page

1. Initialization

We recommend initializing the SDK in the Application onCreate method.

The purpose of SDK initialization is to fetch carrier configuration in advance. Fetching carrier configuration in advance helps improve one-click login speed.

@Override public void onCreate() { super.onCreate(); JVerificationInterface.init(this, new RequestCallback<String>() { @Override public void onResult(int code, String result) { } }); }
          @Override
public void onCreate() {
    super.onCreate();
    JVerificationInterface.init(this, new RequestCallback<String>() {
     @Override
     public void onResult(int code, String result) {
     }
    });
}

        
This code block is shown in the floating window

1. Obtain Carrier Prefetch Number

API: Prefetch Number

Prefetch number is a prerequisite step for obtaining the login token from the carrier. Therefore, after entering the page where one-click login is needed, we recommend calling the prefetch number API first. This reduces prefetch loading time when the user actually uses one-click login and improves user experience.

We recommend adding a field to store the prefetch number API status.

@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Recommended: obtain prefetch number when the page loads preLogin(); } // Add a field to store prefetch number API status private boolean finishPrelogin = true; private void preLogin() { // Check SDK initialization status if (JVerificationInterface.isInitSuccess()){ this.finishPrelogin = false; JVerificationInterface.preLogin(this, 5000, new PreLoginListener() { @Override public void onResult(final int code, final String content, JSONObject operatorReturn) { // Callback response result. We recommend adding a field to store prefetch number API status. this.finishPrelogin = true; } }); } }
          
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 // Recommended: obtain prefetch number when the page loads
 preLogin();
}

// Add a field to store prefetch number API status
private boolean finishPrelogin = true;
private void preLogin() {
 // Check SDK initialization status
 if (JVerificationInterface.isInitSuccess()){
         this.finishPrelogin = false;
 JVerificationInterface.preLogin(this, 5000, new PreLoginListener() {
 @Override
 public void onResult(final int code, final String content, JSONObject operatorReturn) {
 // Callback response result. We recommend adding a field to store prefetch number API status.
 this.finishPrelogin = true;
 }
 });
 }
}

        
This code block is shown in the floating window

API Description

Parameter Type Meaning Description
context Context Context Application context
timeOut int Timeout Valid value range: (0,10000]. If less than or equal to 0, the default value 5000 is used. If greater than 10000, 10000 is used. To ensure prefetch success rate, setting 3000-5000 ms is recommended.
listener PreLoginListener API callback Response callback for prefetch number result

2. Set Authorization Page UI Style

API: Customize Authorization Page UI Style

Before opening the authorization login page, it is recommended to set a custom page style for better page presentation.

private void loginAuth() { // Some pre-operations, such as checking whether prefetch number is in progress, loading HUD, etc............... // Recommended: set custom UI before opening the authorization page setUIConfig(); // The following is the part for opening the authorization page.............. } private void setUIConfig() { // UIConfig example. For details, see the API document JVerifyUIConfig one-click login configuration element description. JVerifyUIConfig uiConfig = new JVerifyUIConfig.Builder().setAuthBGImgPath("main_bg").setNavColor(0xff0086d0).setNavText("登录").setNavTextColor(0xffffffff).setNavReturnImgPath("umcsdk_return_bg").setLogoWidth(70).setLogoHeight(70).setLogoHidden(false).setNumberColor(0xff333333).setLogBtnText("本机号码一键登录").setLogBtnTextColor(0xffffffff).setLogBtnImgPath("umcsdk_login_btn_bg").setPrivacyNameAndUrlBeanList(List<PrivacyBean>).setPrivacyNameAndUrlBeanList(List<PrivacyBean>).setUncheckedImgPath("umcsdk_uncheck_image").setCheckedImgPath("umcsdk_check_image").setSloganTextColor(0xff999999).setLogoOffsetY(50).setLogoImgPath("logo_cm").setNumFieldOffsetY(170).setSloganOffsetY(230).setLogBtnOffsetY(254).setNumberSize(18).setPrivacyState(false).setNavTransparent(false).addCustomView(mBtn, true, new JVerifyUIClickCallback() { @Override public void onClicked(Context context, View view) {Toast.makeText(context,"动态注册的其他按钮",Toast.LENGTH_SHORT).show();} }).addCustomView(mBtn2, false, new JVerifyUIClickCallback() { @Override public void onClicked(Context context, View view) {Toast.makeText(context,"动态注册的其他按钮 222",Toast.LENGTH_SHORT).show();} }).addNavControlView(navBtn, new JVerifyUIClickCallback() { @Override public void onClicked(Context context, View view) {Toast.makeText(context,"导航栏按钮点击",Toast.LENGTH_SHORT).show();} }).setPrivacyOffsetY(30).build(); // Set custom page UI JVerificationInterface.setCustomUIWithConfig(uiConfig); }
          
private void loginAuth() {
        // Some pre-operations, such as checking whether prefetch number is in progress, loading HUD, etc...............
        // Recommended: set custom UI before opening the authorization page
        setUIConfig();
        // The following is the part for opening the authorization page..............
}

private void setUIConfig() {
        // UIConfig example. For details, see the API document JVerifyUIConfig one-click login configuration element description.
        JVerifyUIConfig uiConfig = new JVerifyUIConfig.Builder().setAuthBGImgPath("main_bg").setNavColor(0xff0086d0).setNavText("登录").setNavTextColor(0xffffffff).setNavReturnImgPath("umcsdk_return_bg").setLogoWidth(70).setLogoHeight(70).setLogoHidden(false).setNumberColor(0xff333333).setLogBtnText("本机号码一键登录").setLogBtnTextColor(0xffffffff).setLogBtnImgPath("umcsdk_login_btn_bg").setPrivacyNameAndUrlBeanList(List<PrivacyBean>).setPrivacyNameAndUrlBeanList(List<PrivacyBean>).setUncheckedImgPath("umcsdk_uncheck_image").setCheckedImgPath("umcsdk_check_image").setSloganTextColor(0xff999999).setLogoOffsetY(50).setLogoImgPath("logo_cm").setNumFieldOffsetY(170).setSloganOffsetY(230).setLogBtnOffsetY(254).setNumberSize(18).setPrivacyState(false).setNavTransparent(false).addCustomView(mBtn, true, new JVerifyUIClickCallback() {
 @Override
 public void onClicked(Context context, View view) {Toast.makeText(context,"动态注册的其他按钮",Toast.LENGTH_SHORT).show();}
 }).addCustomView(mBtn2, false, new JVerifyUIClickCallback() {
 @Override
 public void onClicked(Context context, View view) {Toast.makeText(context,"动态注册的其他按钮 222",Toast.LENGTH_SHORT).show();}
 }).addNavControlView(navBtn, new JVerifyUIClickCallback() {
 @Override
 public void onClicked(Context context, View view) {Toast.makeText(context,"导航栏按钮点击",Toast.LENGTH_SHORT).show();}
 }).setPrivacyOffsetY(30).build();

        // Set custom page UI
        JVerificationInterface.setCustomUIWithConfig(uiConfig);
}


        
This code block is shown in the floating window

3. Open Authorization Page

API: Open Authorization Page

Open the authorization page and complete one-click login on this page.

Notes

If prefetch number has not been obtained, this API actively obtains prefetch number and then opens the page.

If the prefetch number API has been called but has not yet completed when this API is called, the callback returns a prompt indicating that prefetch number is being obtained. It is recommended to call this API after prefetch number is complete.

If no custom UI is set, the default UI is used.

private void loginAuth() { // If pre-login is not complete, calling one-click login is not recommended. According to our data analysis, if pre-login does not complete for a long time, the one-click login success rate also decreases. Therefore, the best user experience is to perform one-click login after the pre-login API finishes. // We strongly recommend calling the prefetch number API on the page before one-click login. if (!finishPrelogin) { return; } // Set custom UI setUIConfig(); // LoginSettings is the login configuration LoginSettings settings = new LoginSettings(); settings.setAutoFinish(true);// Set whether to automatically close the authorization page after login is complete settings.setTimeout(15 * 1000);// Set timeout in milliseconds. Valid range: (0,30000]. Values outside the range default to 10000. settings.setAuthPageEventListener(new AuthPageEventListener() { @Override public void onEvent(int cmd, String msg) {//do something...} });// Set authorization page event listener // Open authorization page JVerificationInterface.loginAuth(this, settings, new VerifyListener() { @Override public void onResult(int code, String content, String operator, JSONObject operatorReturn) { // Callback response result of authorization page // Obtain loginToken to complete login } }); } private void setUIConfig() {...... }
          
private void loginAuth() {
     // If pre-login is not complete, calling one-click login is not recommended. According to our data analysis, if pre-login does not complete for a long time, the one-click login success rate also decreases. Therefore, the best user experience is to perform one-click login after the pre-login API finishes.
     // We strongly recommend calling the prefetch number API on the page before one-click login.
     if (!finishPrelogin) {
      return;
      }

 // Set custom UI
 setUIConfig();
 
 // LoginSettings is the login configuration
 LoginSettings settings = new LoginSettings();
 settings.setAutoFinish(true);// Set whether to automatically close the authorization page after login is complete
 settings.setTimeout(15 * 1000);// Set timeout in milliseconds. Valid range: (0,30000]. Values outside the range default to 10000.
 settings.setAuthPageEventListener(new AuthPageEventListener() {
 @Override
 public void onEvent(int cmd, String msg) {//do something...}
 });// Set authorization page event listener
 
 // Open authorization page
 JVerificationInterface.loginAuth(this, settings, new VerifyListener() {
 @Override
 public void onResult(int code, String content, String operator, JSONObject operatorReturn) {
     // Callback response result of authorization page
     // Obtain loginToken to complete login

 }
 });
}

private void setUIConfig() {......
}

        
This code block is shown in the floating window

API Description

Parameter Type Meaning Description
context Context Context Application context
settings LoginSettings Login API settings Used to configure whether to automatically close the page after login completion and other settings
listener VerifyListener API callback Response callback for login result
Was this document helpful?

Copyright 2011-2026, jiguang.cn, All Rights Reserved. 粤ICP备12056275号-13 Shenzhen Hexun Huagu Information Technology Co., Ltd.

Open in Docs Center