Android - 自动填充另一个应用程序的文本字段
Android - Automatically fill text fields of another app
我正在实施一个 Android 应用程序,该应用程序负责与其他服务(例如凭据)进行一些数据交换。然后我想使用该信息自动填写设备上其他应用程序(如 Spotify)的输入字段。
有什么方法可以填写另一个应用程序的输入字段,例如用户名和密码,以消除用户手动输入的苦差事?
我还注意到,至少在 iOS,Spotify 识别出要安装的 1Password 并在输入字段旁边显示一个小图标,我可以用它填充存储在 1Password 中的数据中的字段 - 怎么样这样做是因为它似乎是我问题的另一种解决方案?
提前致谢
您可能想要实施自动填充服务https://developer.android.com/guide/topics/text/autofill-services.html
有一个随时可用的示例应用程序可以帮助您入门 https://github.com/googlesamples/android-AutofillFramework
Android 将调用 onFillRequest()
方法,让您的服务有机会显示自动填充建议。这是上面的示例代码 link:
@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;
}
我正在实施一个 Android 应用程序,该应用程序负责与其他服务(例如凭据)进行一些数据交换。然后我想使用该信息自动填写设备上其他应用程序(如 Spotify)的输入字段。
有什么方法可以填写另一个应用程序的输入字段,例如用户名和密码,以消除用户手动输入的苦差事?
我还注意到,至少在 iOS,Spotify 识别出要安装的 1Password 并在输入字段旁边显示一个小图标,我可以用它填充存储在 1Password 中的数据中的字段 - 怎么样这样做是因为它似乎是我问题的另一种解决方案?
提前致谢
您可能想要实施自动填充服务https://developer.android.com/guide/topics/text/autofill-services.html
有一个随时可用的示例应用程序可以帮助您入门 https://github.com/googlesamples/android-AutofillFramework
Android 将调用 onFillRequest()
方法,让您的服务有机会显示自动填充建议。这是上面的示例代码 link:
@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;
}