【问题标题】:Android - Automatically fill text fields of another appAndroid - 自动填充另一个应用程序的文本字段
【发布时间】:2018-08-29 22:01:26
【问题描述】:

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

有什么方法可以填写其他应用的输入字段,例如用户名和密码,以消除用户手动输入的繁琐工作吗?

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

提前致谢

【问题讨论】:

标签: android textfield autofill


【解决方案1】:

您可能想要实现自动填充服务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;
}

【讨论】:

  • 如果我能解决我的问题,我会看看它。已经非常感谢了
  • 什么是parseStructure和fetchUserData无法解决这些
  • 这段代码中的parseStructure方法是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
  • 1970-01-01
  • 1970-01-01
  • 2018-06-28
  • 1970-01-01
  • 2011-02-25
相关资源
最近更新 更多