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以上纔可以

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章