我正在实施一个Android应用,负责与其他服务(如凭据)进行一些数据交换。然后,我想使用该信息自动填写设备上其他应用程序的输入字段,例如Spotify。
有没有办法填写其他应用的输入字段,比如用户名和密码来删除用户手动输入的杂项?
另外我注意到至少在iOS上,Spotify识别出要安装1Password并在输入字段旁边显示一个小图标,我可以用1Password中存储的数据填充字段 - 这是如何完成的是我问题的另一种解决方案吗?
提前致谢
答案 0 :(得分:3)
您可能希望实施自动填充服务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;
}