Android Oreo:我应该怎么做才能将我的应用发布为自动填充服务提供商? [英] Android Oreo: what should I do to publish my app as an Autofill service provider?

查看:86
本文介绍了Android Oreo:我应该怎么做才能将我的应用发布为自动填充服务提供商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是密码管理器应用程序的独立开发人员.要使我的应用成为自动填充服务提供商(在具有Android Oreo API> = 26的设备中),我应该怎么做,或者应该实现什么(接口/API/服务)?

I'm an independent developer of a password manager app. What should I do, or what should I implement (interface/API/Service), to make my app an Autofill service provider (in a device with Android Oreo API >= 26)?

我已经阅读了各种相关文档,但是我不明白该怎么做.我想念什么吗?

I have read all kinds of related documentation, but I can't understand how to do this. Am I missing something?

此刻,我看到只有知名的密码管理器支持此功能:

At the moment I see that only well-known password managers support this feature:

欢迎任何提示.

推荐答案

和往常一样,Google自己的文档中的描述,我们要创建自动填充服务,它将处理来自其他应用程序(客户端)的请求,以存储和检索自动填充字段数据.

As usual, Google's own examples repository provides a good starting point for learning the Autofill Framework's API, and covers much more material than I can fit into an answer. Here's an overview of the key concepts. From the description in the documentation, we want to create an Autofill service that will handle requests from other apps (the clients) to store and retrieve Autofill field data.

首先,我们需要创建一个履行此合同的服务提供者类.我们可以扩展基础 AutofillService:

First, we need to create a service provider class that fulfills this contract. We can extend the base AutofillService class:

import android.service.autofill.AutofillService;
...
public class MyAutofillService extends AutofillService {
    ...
    @Override
    public void onFillRequest(FillRequest request, CancellationSignal cancellationSignal,
        FillCallback callback) { ... }

    @Override
    public void onSaveRequest(SaveRequest request, SaveCallback callback) { ... }
}

服务的

The service's onFillRequest() and onSaveRequest() methods are the most significant for our understanding. The Android system calls onFillRequest() to determine if our service can autofill fields for a particular activity, and gives the method a FillRequest which contains the context and view information that our service will examine for fillable fields. When the service finishes, it invokes the provided callback with the appropriate autofill data.

以下是为FillRequest提供自动填充建议所需的基本步骤的简化概述:

Here's a dramatically simplified overview of the basic steps needed to provide autofill suggestions for a FillRequest:

@Override
public void onFillRequest(FillRequest request, CancellationSignal signal, FillCallback callback) {
    List<FillContext> contexts = request.getFillContexts();
    AssistStructure structure = contexts.get(contexts.size() - 1);
    WindowNode windowNode = structure.getWindowNodeAt(0);
    ViewNode viewNode = windowNode.getRootViewNode(); // pretend this is an EditText

    String suggestionText = "This will appear in the autofill list for 'viewNode'.";
    RemoteViews suggestion = new RemoteViews(getPackageName(), R.layout.autofill_suggestion);
    suggestion.setTextViewText(R.id.autofill_suggestion, suggestionText);

    Dataset suggestionDataset = new Dataset.Builder(suggestion) 
        .setValue(viewNode.getAutoFillId(), AutofillValue.forText(suggestionText))
        .build();

    FillResponse response = new FillResponse.Builder() 
        .addDataset(suggestionDataset)
        .build();

    callback.onSuccess(response);
}

我们可以看到,自动填充API需要大量代码才能为我们已经预先知道的View提供单个静态自动填充建议,该示例假定viewNode是一个文本输入字段,我们想要提供自动填充建议.实际上,这个例子太简单了,但是我想清楚地说明最小的实现.对于每个WindowNode,我们都需要遍历根 FillResponse.Builder.addDataset() .此示例未显示用于创建RemoteViews的建议显示项的R.layout.autofill_suggestion TextView的纯XML布局.

As we can see, the Autofill API requires a lot of code just to provide a single, static autofill suggestion for a View that we already know in advance—the example assumes that viewNode is a text input field that we want to provide autofill suggestions for. In reality, this example is too simple, but I wanted to clearly show the minimum implementation. For each WindowNode, we need to walk through the view tree of the root ViewNode and each of its children to find each of the input fields that our service wishes to provide autofill data for, and then create a RemoteViews and Dataset that contains the autofill suggestion for each field that we'll add to the FillResponse using FillResponse.Builder.addDataset(). This example doesn't show the plain XML layout for the R.layout.autofill_suggestion TextView used to create the suggestion display item for a RemoteViews.

类似地,当用户希望将数据保存在活动的字段中以供将来完成请求时,Android调用onSaveRequest()并注入

Similarly, Android calls onSaveRequest() when a user wants to save the data in an activity's fields for future completion requests and injects a SaveRequest that our service uses to find autofill data to remember.

每种方法的具体实现将取决于我们的应用提供的自动填充数据的类型.自动填充服务必须认真检查每个字段的特征,并仔细选择一组适当的自动填充建议,以避免将用户的数据泄露给恶意的客户端活动(请参阅评论).特别是对于密码管理器,在请求和保存自动填充数据时,我们需要特别注意正确验证服务的用户身份并提供安全的建议集.

The specific implementation of each of these methods will depend on the type of Autofill data that our app provides. Autofill services must conscientiously examine the characteristics of each field and carefully select a set of appropriate autofill suggestions to avoid leaking the user's data to a malicious client activity (see comments). For a password manager in particular, we need to pay special attention to properly authenticating a user of the service and providing a safe set of suggestions when requesting and saving autofill data.

我们现在可以在注册服务. >项目 AndroidManifest.xml 的块:

We can now register the service in the <application> block of the project's AndroidManifest.xml:

<service
    android:name=".MyAutofillService"
    android:label="Multi-Dataset Autofill Service"
    android:permission="android.permission.BIND_AUTOFILL_SERVICE">
    <meta-data
        android:name="android.autofill"
        android:resource="@xml/multidataset_service" />

    <intent-filter>
        <action android:name="android.service.autofill.AutofillService" />
    </intent-filter>
</service>

如图所示,这会将我们的自动填充服务绑定为一个可用选项,该选项出现在问题中显示的自动填充服务列表中. android:name属性必须与我们的AutofillService类的名称匹配,并且我们的应用程序必须声明BIND_AUTOFILL_SERVICE权限.将android:label的值更改为该服务的适当名称(例如,使用密码管理器自动填充").或者,在字符串资源中设置它.还要注意,我们应该提供一个设置"活动,用于配置我们在<meta‑data>中为android.autofill指定的服务:

As shown, this binds our Autofill service as an available option that appears in the Autofill services list show in the question. The android:name attribute must match the name of our AutofillService class, and our app must declare the BIND_AUTOFILL_SERVICE permission. Change the value of android:label to a suitable name for the service (for example, "Autofill with Password Manager"). Alternatively, set this in a string resource. Note also that we should provide a "settings" activity used to configure our service which we specify in the <meta‑data> for android.autofill:

<autofill-service android:settingsActivity="foo.bar.SettingsActivity" />

然后,用户可以从其设备设置中启用我们的自动填充服务.我们可以在设置或首次启动过程中广播 ACTION_REQUEST_SET_AUTOFILL_SERVICE 意向协助用户找到此屏幕.

Then, the user can enable our Autofill service from their device Settings. We can broadcast the ACTION_REQUEST_SET_AUTOFILL_SERVICE intent during setup or first launch to assist the user in finding this screen.

这篇关于Android Oreo:我应该怎么做才能将我的应用发布为自动填充服务提供商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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