如何使用 Android 自动填充 API

How to use Android autofill API

我使用 android webview 组件构建了一个小浏览器,并希望使用 Android AutoFill API.[=14 集成 password/credential 管理器支持=]

我已经阅读了文档,但完全迷失了,找不到任何与 webviews 等复杂事物集成的示例。

javascript 这方面对我来说不是问题,我已经有当用户 select 在登录表单(和自动填充对话框)上输入时触发的事件should 显示),当 a 完成输入用户名和密码并提交页面时(并且凭据 should 保存回密码管理器) 但我很难理解这方面的 android。

webview 似乎已经对此有一些基本的支持,例如,如果我长按登录表单输入,并且 select 上下文菜单中的“自动填充”,我可以得到它插入一些保存在凭证管理器中的值。问题是凭据已保存在我的应用程序 ID 中,而不是网站域中,所以我的第一个问题是,当我请求自动填充菜单时,我如何告诉 API 它是针对特定的字段类型(例如 username/password)并属于特定网站,以便它知道要获取哪些凭据并可以稍后更新它们?这是我尝试在 select 在登录表单中输入一个字段时触发自动填充对话框出现的尝试。

更新: 当我在我的应用程序中创建静态 webview 时,自动填充会正确保存并提示表单上的凭据,并在每个站点正确保存它们但我需要它在 recyclerview 中的 webview 中工作,并且出于某种原因它们尽管共享相同设置。我在 recyclerviews https://developer.android.com/guide/topics/text/autofill-optimize#recycle but using setAutofillId() doesn't seem to help and even the official example here seems a bit unreliable when I test it on my phone https://github.com/android/input-samples/blob/master/AutofillFramework/Application/src/main/java/com/example/android/autofill/app/commoncases/RecyclerViewActivity.java

中找到了有关自动填充的信息

有一个 guide available here 描述了如何让您的应用准备好利用自动填充。以下是描述您的选择的最相关部分,尤其是在使用 Webview 时:

In many cases, Autofill may work in your app without any effort. But to ensure consistent behavior, we recommend providing explicit hints to tell the framework about the contents of your field. You can do this using either the android:autofillHints attribute or the setAutofillHints() method.

不过,要将您的应用与网站相关联,您需要向应用的清单添加一些设置。您将在上面 link 的第 2 步中看到它。

You'll need to update your app's manifest file with an asset_statements resource, which links to the URL where your assetlinks.json file is hosted. Once that's done, you'll need to submit your updated app to the Play Store, and fill out the Affiliation Submission Form for the association to go live.

When using Android Studio 3.0, the App Links Assistant can generate all of this for you. When you open the DAL generator tool (Tools -> App Links Assistant -> Open Digital Asset Links File Generator), simply make sure you enable the new checkbox labeled "Support sharing credentials between the app and website". Similarly, with WebViews in your apps, you can use HTML Autocomplete Attributes to provide hints about fields. Autofill will work in WebViews as long as you have Chrome 61 or later installed on your device. Even if your app is using custom views, you can also define the metadata that allows autofill to work.

一个简单的解决方案是让 webview 处理这个。

webView.getSettings().setSaveFormData(true);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(true);
    webView.setWebChromeClient(new WebChromeClient() /*Or yourOwnWebChromeClient*/);
    webView.setWebViewClient(new WebViewClient() /*Or yourOwnWebViewClient*/);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setSaveFormData(true);

    CookieSyncManager.createInstance(this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    CookieSyncManager.getInstance().startSync();

如果需要,您还可以添加 JavaScript 接口

    webView.addJavascriptInterface(this, JSInterface);

我使用了这种方法,它保存了 facebook、instagram 以及 dailymotion.com 的登录令牌。

请试试这个

you may try Creating multiple cache

您可能也想参考这个 - https://developer.android.com/guide/webapps/managing-webview#java

我遇到的问题是我的 webview 嵌入了 recyclerview,要在 recyclerview 的 web 视图上启用自动填充,您必须将其设置为在 recyclerview 上启用,而不是在 webview 本身上启用。