如何为Android应用程序实现密码的智能锁 [英] How to Implement Smart Lock for Passwords to Android Application

查看:506
本文介绍了如何为Android应用程序实现密码的智能锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人有关于Smart Lock的知识吗?
它是如何工作的?



我想开发一个在Android应用程序中为密码实现Smart Lock的应用程序。



我遵循 https://developers.google.com/identity/smartlock -passwords / android /



我已初始化 GoogleApiClient

  mCredentialsApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi( Auth.CREDENTIALS_API)
.build();

以及生成的 Credential
$ b $ pre $ 最终Credential凭证=新Credential.Builder(电子邮件)
.setPassword(密码)
.build() ;

使用凭证API 保存凭证,I使用

  Auth.CredentialsApi.save(mCredentialsClient,凭证).setResultCallback(
新的ResultCallback(){
@Override
public void onResult(状态状态){
if(status.isSuccess()){
//凭证已保存
} else {
if( status.hasResolution()){
//尝试解析保存请求,如果
//凭证是新的,将提示用户
try {
status.startResolutionForResult (this,RC_SAVE);
} catch(IntentSender.SendIntentException e){
//无法解析请求
}
}
}
}
});

我的 Manifest 的权限为



 < uses-permission android:name =android.permission.USE_CREDENTIALS/> 
< uses-permission android:name =android.permission.ACCESS_NETWORK_STATE/>
< uses-permission android:name =android.permission.INTERNET/>
<使用权限android:name =android.permission.GET_ACCOUNTS/>

并且还在应用程序标签内添加了元数据作为

 <元数据
android:name =com.google.android.gms.version
android:value =@ integer / google_play_services_version/>

在Android应用程序中完成所有工作后,我在 Google Developer Console上生成了项目并完成了此处的链接



但是当我运行项目时,出现如下错误:



有没有人在Android上使用Google的Smart Lock for Password应用程序?

解决方案

对于我来说,智能锁定密码在Android上示例项目工作正常。你可能想尝试从那里开始,看看是否有帮助。在示例代码中,保存凭证的实现方式如下:

onCreate

  mCredentialsApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi( Auth.CREDENTIALS_API)
.build();

...
saveCredetial 方法:

 最终凭证凭证=新的Credential.Builder(电子邮件)
.setPassword(密码)
.build();

Auth.CredentialsApi.save(mCredentialsApiClient,凭证).setResultCallback(
新的ResultCallback< Status>(){
@Override
public void onResult(状态状态) {
if(status.isSuccess()){
Log.d(TAG,SAVE:OK);
showToast(凭证保存);
hideProgress() ;
} else {
resolveResult(status,RC_SAVE);
}
}
});

...
resolveResult 方法:

  private void resolveResult(Status status,int requestCode){
Log.d( TAG,解决:+状态);
if(status.hasResolution()){
Log.d(TAG,STATUS:RESOLVING);
尝试{
status.startResolutionForResult(MainActivity.this,requestCode);
} catch(IntentSender.SendIntentException e){
Log.e(TAG,STATUS:Failed to send resolution。,e);
hideProgress();
}
} else {
Log.e(TAG,STATUS:FAIL);
showToast(无法解决错误);
hideProgress();
}
}


Does anyone has knowledge about Smart Lock? How does it works?

I want to develop an application implementing Smart Lock for passwords in Android application.

I am following https://developers.google.com/identity/smartlock-passwords/android/.

I have initialized GoogleApiClient

 mCredentialsApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Auth.CREDENTIALS_API)
            .build();

and generated instance of Credential as

 final Credential credential = new Credential.Builder(email)
            .setPassword(password)
            .build();

to save credentials using Credentials API, I used

Auth.CredentialsApi.save(mCredentialsClient, credential).setResultCallback(
 new ResultCallback() {
  @Override
  public void onResult(Status status) {
      if (status.isSuccess()) {
          // Credentials were saved
      } else {
          if (status.hasResolution()) {
              // Try to resolve the save request. This will prompt the user if
              // the credential is new.
              try {
                  status.startResolutionForResult(this, RC_SAVE);
              } catch (IntentSender.SendIntentException e) {
                  // Could not resolve the request
              }
          }
      }
  }
});

my Manifest has the permission as

   <uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

and also added Meta data inside application tag as

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

after doing all in Android application, I have generated project on Google Developer Console and have done everything as mentioned in this link here.

But when I run project, I am getting an error as:

Could not resolve error.

Has anyone worked on Google's Smart Lock for Password on Android application?

解决方案

For me, the Smart Lock for Passwords on Android sample project worked fine. You might want to try starting from there and see if that helps. In the sample code, save credential is implemented as:

In onCreate:

    mCredentialsApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Auth.CREDENTIALS_API)
            .build();

... In the saveCredetial method:

    final Credential credential = new Credential.Builder(email)
            .setPassword(password)
            .build();

    Auth.CredentialsApi.save(mCredentialsApiClient, credential).setResultCallback(
            new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    if (status.isSuccess()) {
                        Log.d(TAG, "SAVE: OK");
                        showToast("Credential Saved");
                        hideProgress();
                    } else {
                        resolveResult(status, RC_SAVE);
                    }
                }
            });

... The resolveResult method on the main activity:

private void resolveResult(Status status, int requestCode) {
    Log.d(TAG, "Resolving: " + status);
    if (status.hasResolution()) {
        Log.d(TAG, "STATUS: RESOLVING");
        try {
            status.startResolutionForResult(MainActivity.this, requestCode);
        } catch (IntentSender.SendIntentException e) {
            Log.e(TAG, "STATUS: Failed to send resolution.", e);
            hideProgress();
        }
    } else {
        Log.e(TAG, "STATUS: FAIL");
        showToast("Could Not Resolve Error");
        hideProgress();
    }
}

这篇关于如何为Android应用程序实现密码的智能锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆