本文為您介紹Android SDK Demo的運行步驟及示例代碼,幫助您快速了解一鍵登錄及本機號碼校驗功能。有關SDK的詳細接入步驟及用法請參見一鍵登錄 > Android客戶端接入,本機號碼校驗 > Android客戶端接入。
步驟一:下載SDK
登錄號碼認證產品控制臺,在概覽頁面右側API&SDK區域,單擊立即下載,進入API&SDK頁面,根據頁面提示下載并解壓對應SDK。
步驟二:打開Demo
使用Android Studio IDE,選擇打開已有項目,選擇到Demo文件夾。項目載入過程會拉取相關依賴,請耐心等待。
本文使用Android Studio 4.0版本進行演示。如果您使用的是其他的IDE版本,可能會存在構建報錯,請按照提示修改對應Gradle版本。使用工具和環境搭建相關可參考準備工作。
步驟三:設置認證方案
查看簽名
使用Android Studio右側面板,Gradle > 項目名稱 > 簽名文件所在module >Tasks > android > signingReport,查看簽名。
包簽名獲取方式如下:
方式一:上圖的下方輸出的MD5值移除冒號,將大寫字母轉換為小寫字母即可生成包簽名。
方式二:請通過手機終端下載并安裝Android App簽名獲取工具,獲取Android App的包簽名信息。
查看包名
包名在app/build.gradle文件android.defaultConfig.applicationId
屬性。
創建認證方案
登錄號碼認證服務控制臺,根據獲取到的包名,包簽名,創建認證方案。
步驟四:替換密鑰
找到module下的build.gradle文件,用上一步認證方案生成的密鑰替換buildField.AUTH_SECRET
字段。
步驟五:構建及運行
將Android系統的手機連接到電腦上并打開USB調試模式。
點擊IDE頂部工具菜單,構建并運行項目。
功能試用。
說明確保您的終端設備已開啟SIM卡的移動數據網絡(支持中國聯通、中國移動的3G網絡,但接口耗時會增加)。
上圖是Demo項目在手機上的運行效果。
代碼示例
一鍵登錄
package com.aliqin.mytel.login;
import static com.aliqin.mytel.Constant.THEME_KEY;
import static com.aliqin.mytel.uitls.MockRequest.getPhoneNumber;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.aliqin.mytel.BuildConfig;
import com.aliqin.mytel.MessageActivity;
import com.aliqin.mytel.R;
import com.aliqin.mytel.config.BaseUIConfig;
import com.aliqin.mytel.uitls.ExecutorManager;
import com.mobile.auth.gatewayauth.AuthUIConfig;
import com.mobile.auth.gatewayauth.PhoneNumberAuthHelper;
import com.mobile.auth.gatewayauth.ResultCode;
import com.mobile.auth.gatewayauth.TokenResultListener;
import com.mobile.auth.gatewayauth.model.TokenRet;
/**
* 進app直接登錄的場景
*/
public class OneKeyLoginActivity extends Activity {
private static final String TAG = OneKeyLoginActivity.class.getSimpleName();
private EditText mTvResult;
private PhoneNumberAuthHelper mPhoneNumberAuthHelper;
private TokenResultListener mTokenResultListener;
private ProgressDialog mProgressDialog;
private int mUIType;
private BaseUIConfig mUIConfig;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUIType = getIntent().getIntExtra(THEME_KEY, -1);
setContentView(R.layout.activity_login);
mTvResult = findViewById(R.id.tv_result);
sdkInit(BuildConfig.AUTH_SECRET);
mUIConfig = BaseUIConfig.init(mUIType, this, mPhoneNumberAuthHelper);
oneKeyLogin();
mTvResult.setOnLongClickListener(
new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mTvResult.setTextIsSelectable(true);
mTvResult.setSelectAllOnFocus(true);
ClipboardManager cm = (ClipboardManager) getSystemService(
Context.CLIPBOARD_SERVICE
);
// 創建普通字符型ClipData
ClipData mClipData = ClipData.newPlainText(
"Label",
mTvResult.getText()
);
// 將ClipData內容放到系統剪貼板里。
cm.setPrimaryClip(mClipData);
Toast
.makeText(
OneKeyLoginActivity.this,
"登錄token已復制",
Toast.LENGTH_SHORT
)
.show();
return false;
}
}
);
}
public void sdkInit(String secretInfo) {
mTokenResultListener =
new TokenResultListener() {
@Override
public void onTokenSuccess(String s) {
hideLoadingDialog();
if (BuildConfig.NeedLogger) {
Log.e(TAG, "獲取token成功:" + s);
}
TokenRet tokenRet = null;
try {
tokenRet = TokenRet.fromJson(s);
if (
ResultCode.CODE_START_AUTHPAGE_SUCCESS.equals(tokenRet.getCode())
) {
if (BuildConfig.NeedLogger) {
Log.i(TAG, "喚起授權頁成功:" + s);
}
}
if (ResultCode.CODE_SUCCESS.equals(tokenRet.getCode())) {
if (BuildConfig.NeedLogger) {
Log.i(TAG, "獲取token成功:" + s);
}
getResultWithToken(tokenRet.getToken());
mPhoneNumberAuthHelper.setAuthListener(null);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onTokenFailed(String s) {
if (BuildConfig.NeedLogger) {
Log.e(TAG, "獲取token失敗:" + s);
}
hideLoadingDialog();
mPhoneNumberAuthHelper.hideLoginLoading();
TokenRet tokenRet = null;
try {
tokenRet = TokenRet.fromJson(s);
if (ResultCode.CODE_ERROR_USER_CANCEL.equals(tokenRet.getCode())) {
//模擬的是必須登錄 否則直接退出app的場景
finish();
} else {
Toast
.makeText(
getApplicationContext(),
"一鍵登錄失敗切換到其他登錄方式",
Toast.LENGTH_SHORT
)
.show();
Intent pIntent = new Intent(
OneKeyLoginActivity.this,
MessageActivity.class
);
startActivityForResult(pIntent, 1002);
}
} catch (Exception e) {
e.printStackTrace();
}
mPhoneNumberAuthHelper.quitLoginPage();
mPhoneNumberAuthHelper.setAuthListener(null);
}
};
mPhoneNumberAuthHelper =
PhoneNumberAuthHelper.getInstance(this, mTokenResultListener);
mPhoneNumberAuthHelper
.getReporter()
.setLoggerEnable(BuildConfig.NeedLogger);
mPhoneNumberAuthHelper.setAuthSDKInfo(secretInfo);
}
/**
* 進入app就需要登錄的場景使用
*/
private void oneKeyLogin() {
mPhoneNumberAuthHelper.checkEnvAvailable();
mUIConfig.configAuthPage();
//用戶控制返回鍵及左上角返回按鈕效果
mPhoneNumberAuthHelper.userControlAuthPageCancel();
//用戶禁用utdid
//mPhoneNumberAuthHelper.prohibitUseUtdid();
//授權頁是否跟隨系統深色模式
mPhoneNumberAuthHelper.setAuthPageUseDayLight(true);
//授權頁物理返回鍵禁用
//mPhoneNumberAuthHelper.closeAuthPageReturnBack(true);
//橫屏水滴屏全屏適配
mPhoneNumberAuthHelper.keepAuthPageLandscapeFullSreen(true);
//授權頁擴大協議按鈕選擇范圍至我已閱讀并同意
mPhoneNumberAuthHelper.expandAuthPageCheckedScope(true);
getLoginToken(5000);
}
/**
* 拉起授權頁
* @param timeout 超時時間
*/
public void getLoginToken(int timeout) {
mPhoneNumberAuthHelper.getLoginToken(this, timeout);
showLoadingDialog("正在喚起授權頁");
}
public void getResultWithToken(final String token) {
ExecutorManager.run(
new Runnable() {
@Override
public void run() {
final String result = getPhoneNumber(token);
OneKeyLoginActivity.this.runOnUiThread(
new Runnable() {
@Override
public void run() {
mTvResult.setText("登錄成功:" + result);
mTvResult.setMovementMethod(
ScrollingMovementMethod.getInstance()
);
mPhoneNumberAuthHelper.quitLoginPage();
}
}
);
}
}
);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1002) {
if (resultCode == 1) {
mTvResult.setText("登錄成功:" + data.getStringExtra("result"));
mTvResult.setMovementMethod(ScrollingMovementMethod.getInstance());
} else {
//模擬的是必須登錄 否則直接退出app的場景
finish();
}
}
}
public void showLoadingDialog(String hint) {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
}
mProgressDialog.setMessage(hint);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
}
public void hideLoadingDialog() {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
}
@Override
protected void onResume() {
super.onResume();
mUIConfig.onResume();
}
}
本機號碼校驗
package com.aliqin.mytel.auth;
import static com.aliqin.mytel.uitls.MockRequest.verifyNumber;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.Nullable;
import com.aliqin.mytel.BuildConfig;
import com.aliqin.mytel.R;
import com.aliqin.mytel.uitls.ExecutorManager;
import com.mobile.auth.gatewayauth.PhoneNumberAuthHelper;
import com.mobile.auth.gatewayauth.PreLoginResultListener;
import com.mobile.auth.gatewayauth.ResultCode;
import com.mobile.auth.gatewayauth.TokenResultListener;
import com.mobile.auth.gatewayauth.model.TokenRet;
public class NumberAuthActivity extends Activity {
private static final String TAG = NumberAuthActivity.class.getSimpleName();
private PhoneNumberAuthHelper mPhoneNumberAuthHelper;
private TokenResultListener mVerifyListener;
private Button mAuthButton;
private EditText mNumberEt;
private String phoneNumber;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);
mAuthButton = findViewById(R.id.auth_btn);
mNumberEt = findViewById(R.id.et_number);
mAuthButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
phoneNumber = mNumberEt.getText().toString();
//判斷手機號是否合法
if (!TextUtils.isEmpty(phoneNumber)) {
showLoadingDialog("正在進行本機號碼校驗");
numberAuth(5000);
}
}
}
);
sdkInit();
accelerateVerify(5000);
}
private void sdkInit() {
mVerifyListener =
new TokenResultListener() {
@Override
public void onTokenSuccess(String s) {
if (BuildConfig.NeedLogger) {
Log.i(TAG, "獲取token成功:" + s);
}
try {
TokenRet pTokenRet = TokenRet.fromJson(s);
if (
ResultCode.CODE_SUCCESS.equals(pTokenRet.getCode()) &&
!TextUtils.isEmpty(pTokenRet.getToken())
) {
getResultWithToken(pTokenRet.getToken(), phoneNumber);
}
mPhoneNumberAuthHelper.setAuthListener(null);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onTokenFailed(final String s) {
if (BuildConfig.NeedLogger) {
Log.i(TAG, "獲取token失敗:" + s);
}
NumberAuthActivity.this.runOnUiThread(
new Runnable() {
@Override
public void run() {
hideLoadingDialog();
setResult(2);
finish();
}
}
);
mPhoneNumberAuthHelper.setAuthListener(null);
}
};
mPhoneNumberAuthHelper =
PhoneNumberAuthHelper.getInstance(
getApplicationContext(),
mVerifyListener
);
mPhoneNumberAuthHelper
.getReporter()
.setLoggerEnable(BuildConfig.NeedLogger);
}
/**
* 加速校驗
* 進入輸入手機號頁面調用此接口,用戶輸入完手機號點擊確定可以更快的獲取token
* @param timeout
*/
public void accelerateVerify(int timeout) {
mPhoneNumberAuthHelper.accelerateVerify(
timeout,
new PreLoginResultListener() {
@Override
public void onTokenSuccess(String vendor) {
//成功時返回運營商簡稱
if (BuildConfig.NeedLogger) {
Log.i(TAG, "accelerateVerify:" + vendor);
}
}
@Override
public void onTokenFailed(String vendor, String errorMsg) {
if (BuildConfig.NeedLogger) {
Log.e(TAG, "accelerateVerify:" + vendor + ", " + errorMsg);
}
}
}
);
}
public void numberAuth(int timeout) {
mPhoneNumberAuthHelper.setAuthListener(mVerifyListener);
mPhoneNumberAuthHelper.getVerifyToken(timeout);
}
public void getResultWithToken(final String token, final String phoneNumber) {
ExecutorManager.run(
new Runnable() {
@Override
public void run() {
final String result = verifyNumber(token, phoneNumber);
NumberAuthActivity.this.runOnUiThread(
new Runnable() {
@Override
public void run() {
hideLoadingDialog();
Intent pIntent = new Intent();
pIntent.putExtra("result", phoneNumber);
setResult(1, pIntent);
finish();
}
}
);
}
}
);
}
public void showLoadingDialog(String hint) {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
}
mProgressDialog.setMessage(hint);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
}
public void hideLoadingDialog() {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
}
}