[英]How to use smart lock API in my application to unlock pattern mode?
它可能有點復雜,但谷歌已經提供了大量有關此用法的文檔 。
要請求存儲的憑據,您必須創建一個配置為訪問Credentials API的GoogleApiClient
實例。
mCredentialsApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.enableAutoManage(this, this)
.addApi(Auth.CREDENTIALS_API)
.build();
CredentialRequest對象指定要從中請求憑據的登錄系統。 使用setPasswordLoginSupported
方法為基於密碼的登錄構建CredentialRequest
,並為聯合登錄服務(如Google登錄setAccountTypes()
方法。
mCredentialRequest = new CredentialRequest.Builder()
.setPasswordLoginSupported(true)
.setAccountTypes(IdentityProviders.GOOGLE, IdentityProviders.TWITTER)
.build();
創建GoogleApiClient
和CredentialRequest
對象后,將它們傳遞給CredentialsApi.request()
方法,以請求為您的應用程序存儲的憑據。
Auth.CredentialsApi.request(mCredentialsClient, mCredentialRequest).setResultCallback(
new ResultCallback<CredentialRequestResult>() {
@Override
public void onResult(CredentialRequestResult credentialRequestResult) {
if (credentialRequestResult.getStatus().isSuccess()) {
// See "Handle successful credential requests"
onCredentialRetrieved(credentialRequestResult.getCredential());
} else {
// See "Handle unsuccessful and incomplete credential requests"
resolveResult(credentialRequestResult.getStatus());
}
}
});
在成功的憑據請求中,使用生成的憑據對象完成用戶對您的應用程序的登錄。 使用getAccountType()
方法確定檢索到的憑據的類型,然后完成相應的登錄過程。
private void onCredentialRetrieved(Credential credential) {
String accountType = credential.getAccountType();
if (accountType == null) {
// Sign the user in with information from the Credential.
signInWithPassword(credential.getId(), credential.getPassword());
} else if (accountType.equals(IdentityProviders.GOOGLE)) {
// The user has previously signed in with Google Sign-In. Silently
// sign in the user with the same ID.
// See https://developers.google.com/identity/sign-in/android/
GoogleSignInOptions gso =
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.setAccountName(credential.getId())
.build();
OptionalPendingResult<GoogleSignInResult> opr =
Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
// ...
}
}
我認為沒有SmartLock API。 就像Pravin在評論中所說,智能鎖將在設備連接時自動禁用模式。
我沒有試過這個,但是一旦模式被禁用,你應該能夠繞過鎖定屏幕(從這個答案 ):
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
您需要為清單添加權限:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.