16:跳过密码保存,因为用户可能会收到 Android Autofill 提示

问题描述 投票:0回答:2

我在我的 Android 应用程序中集成了谷歌智能锁,但在某些设备中,我在尝试将凭据保存到谷歌时遇到此错误。我正在使用以下代码来保存凭据 -

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

在谷歌上搜索此解决方案后发现需要在应用程序中禁用自动填充功能以保存密码。

尝试 1 - 将以下代码放入活动中以在特定活动中提交 autofillmager 并禁用自动填充。但它不起作用。

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    AutofillManager autofillManager = getSystemService(AutofillManager.class);
    autofillManager.commit();

    getWindow()
            .getDecorView()
            .setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
 }

尝试 2 - 在

EditText

的属性中添加以下内容
 android:longClickable="false"

longClickable 应该停止自动填充,但它不起作用。

 android:importantForAutofill="no"

也尝试使用

importantForAutoFill
但它也不起作用。

android android-8.0-oreo autofill google-smartlockpasswords
2个回答
0
投票

最后我找到了解决方法。出现此错误是因为系统自动填充正试图超越您的谷歌自动填充实现..

所以首先我们必须禁用 Android 操作系统尝试自动填充输入字段并禁用我们应用程序的自动填充。

为了禁用自动填充,我做了两件事..

我尝试通过以下 xml 属性禁用系统自动填充,但根本不起作用。

android:importantForAutofill="否"

根本不起作用..然后我找到了以下答案。建议实现自定义 EditText(或任何输入类型)并禁用来自源的自动填充尝试。如下实现您自己的 EdiText 版本。

public class EditTextWithNoAutoFill extends androidx.appcompat.widget.AppCompatEditText {

    public EditTextWithNoAutoFill(@NonNull Context context) {
        super(context);
    }

    public EditTextWithNoAutoFill(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextWithNoAutoFill(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public int getAutofillType() {
        return View.AUTOFILL_TYPE_NONE;
    }

    @Override
    public int getImportantForAutofill() {
        return IMPORTANT_FOR_AUTOFILL_NO;
    }
}

}

Fixe 在于这两个方法实现 'getImportantForAutofill()' & 'getAutofillType()'

然后我也应用了这个..

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            getWindow().getDecorView().setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
        }

应用这两个更改并运行它后,并没有立即为我解决问题..我不得不重新启动正在运行的设备/清理构建缓存/重新启动 android studio(不确定哪个选项触发了修复)

经过上述更改后一切正常。

有关禁用操作系统“自动填充”的更多参考,我将此参考留在原始答案

https://stackoverflow.com/a/46698028/9901309


-1
投票

我删除了所有手动保存的代码并添加了 登录表单中的登录字段为

android:autofillHints="username"
,密码字段为
android:autofillHints="password"
。多亏了 Android 的自动填充服务,密码保存对话框仍然出现。

© www.soinside.com 2019 - 2024. All rights reserved.