Edittext的自动填充引起的Bug

最近发现线上有一个bug:

    at com.android.internal.util.SyncResultReceiver.waitResult(SyncResultReceiver.java:60)
    at com.android.internal.util.SyncResultReceiver.getIntResult(SyncResultReceiver.java:68)
    at android.view.autofill.AutofillManager.ensureServiceClientAddedIfNeededLocked(AutofillManager.java:1847)
    at android.view.autofill.AutofillManager.notifyViewEnteredLocked(AutofillManager.java:966)
    at android.view.autofill.AutofillManager.notifyViewEntered(AutofillManager.java:950)
    at android.view.autofill.AutofillManager.notifyViewEntered(AutofillManager.java:901)
    ...

并且只出现在Android10上,经查询原来是Edittext默认开启自动填充功能引起的,本片就简单了解一下自动Edittext自动填充功能,以及相应bug的解决方式。

背景

在Android 8.0中加入了Autofill framework,也就是自动填充框架,目的在于简化了登录和信用卡表单之类表单的填写工作。如果一个应用支持自动填充,系统从用户的Google账户保存的自动填充信息中选取相应的内容来填充当前输入框,比如下图中,一个登录页面的手机号码输入栏,自动填充的推荐内容则是当前用户的手机号码。

使用

作为一个开发者,如何来控制自动填充功能呢?在Android O Developer Preview 3中google提供了一些解决办法,即从sdk26开始,所有的视图中新增了一个android:importantForAutofill的属性,以及相应的一个方法setImportantForAutofill()来控制自动填充功能。

当然我们也可以在代码中(Activity的onCreat中)强制请求自动填充:

public void forceAutofill() {
 AutofillManager afm = context.getSystemService(AutofillManager.class);
 if (afm!= null) {
    afm.requestAutofill();
 }

解决Bug

自动填充功能很好,但是在某些安全性要求较高或者某些特殊的页面,自动填充不再是一个优势,反而带来影响的时候,另外开篇出现的bug是在Android10默认开启自动填充功能引起的,所以针对这些我们一般会屏蔽自动填充功能。屏蔽方法也很简单:

1.在XML中实现

android:importantForAutofill="no"

2.在代码中实现



 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     disableAutoFill();
 }

 @TargetApi(Build.VERSION_CODES.O)
 private void disableAutoFill() { 
     getWindow().getDecorView().setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
 }

这里我建议第二种直接写在BaseActivity中,切记判断版本Android O以上才可以

发布了47 篇原创文章 · 获赞 38 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/li0978/article/details/105014121