Android - 自动填充另一个应用程序的文本字段

pho*_*bus 2 android autofill textfield

我正在实现一个 Android 应用程序,它负责与其他服务(如凭据)进行一些数据交换。然后我想使用该信息自动填写设备上其他应用程序(如 Spotify)的输入字段。

有什么办法可以填写另一个应用程序的输入字段,比如用户名和密码,以消除用户手动输入的麻烦?

我还注意到,至少在 iOS 上,Spotify 识别出要安装的 1Password 并在输入字段旁边显示一个小图标,我可以用它来填充 1Password 中存储的数据中的字段 - 这是如何完成的,因为它似乎是另一种解决方案我的问题?

提前致谢

M-W*_*eEh 5

您可能想要实现自动填充服务https://developer.android.com/guide/topics/text/autofill-services.html

有一个随时可用的示例应用程序,可以帮助您入门https://github.com/googlesamples/android-AutofillFramework

Android 将调用onFillRequest()方法,让您的服务有机会显示自动填充建议。这是来自上面链接的示例代码:

@Override
public void onFillRequest(FillRequest request, CancellationSignal cancellationSignal, FillCallback callback) {
    // Get the structure from the request
    List<FillContext> context = request.getFillContexts();
    AssistStructure structure = context.get(context.size() - 1).getStructure();

    // Traverse the structure looking for nodes to fill out.
    ParsedStructure parsedStructure = parseStructure(structure);

    // Fetch user data that matches the fields.
    UserData userData = fetchUserData(parsedStructure);

    // Build the presentation of the datasets
    RemoteViews usernamePresentation = new RemoteViews(getPackageName(), android.R.layout.simple_list_item_1);
    usernamePresentation.setTextViewText(android.R.id.text1, "my_username");
    RemoteViews passwordPresentation = new RemoteViews(getPackageName(), android.R.layout.simple_list_item_1);
    passwordPresentation.setTextViewText(android.R.id.text1, "Password for my_username");

    // Add a dataset to the response
    FillResponse fillResponse = new FillResponse.Builder()
            .addDataset(new Dataset.Builder()
                    .setValue(parsedStructure.usernameId,
                            AutofillValue.forText(userData.username), usernamePresentation)
                    .setValue(parsedStructure.passwordId,
                            AutofillValue.forText(userData.password), passwordPresentation)
                    .build())
            .build();

    // If there are no errors, call onSuccess() and pass the response
    callback.onSuccess(fillResponse);
}

class ParsedStructure {
    AutofillId usernameId;
    AutofillId passwordId;
}

class UserData {
    String username;
    String password;
}
Run Code Online (Sandbox Code Playgroud)

  • 什么是 parseStructure 和 fetchUserData 无法解析这些 (2认同)