Android AutofillService保存密码时仅显示星号的问题及解决
问题原因
Android出于用户隐私安全的保护机制,AssistStructure中密码输入框的getText()方法返回的是界面上显示的掩码(星号/圆点),而非用户实际输入的明文密码。这是系统防止恶意服务窃取用户敏感信息的安全限制。
解决方法
要获取明文密码,需要通过ViewNode的autofillValue属性——这是系统专门为合法的AutofillService提供的安全获取渠道。
修改你的代码如下:
List<FillContext> contexts = request.getFillContexts(); AssistStructure structure = contexts.get(contexts.size() - 1).getStructure(); ParsedStructure parsedStructure = ParsedStructure.parse(structure); // 获取密码输入框的AutofillValue AutofillValue passwordAutofillValue = parsedStructure.getPasswordView().getAutofillValue(); // 从AutofillValue中提取明文密码 String password = passwordAutofillValue != null ? passwordAutofillValue.getTextValue().toString() : "";
注意事项
- 只有当用户触发了系统的密码保存提示,且你的AutofillService是用户设置的默认自动填充服务时,
autofillValue才会包含明文密码,否则该值可能为null。 - 务必遵循Android的Autofill安全规范,不要将获取到的明文密码用于非授权用途,避免违反隐私政策。
内容的提问来源于stack exchange,提问作者user14152955
相关产品推荐
相关产品推荐

