1. 程式人生 > >如何關閉Google的自動填充功能

如何關閉Google的自動填充功能

遇到問題是這樣的

05-02 14:58:57.154 15241 15241 E AndroidRuntime: FATAL EXCEPTION: main
05-02 14:58:57.154 15241 15241 E AndroidRuntime: Process: com.android.phone, PID: 15241
05-02 14:58:57.154 15241 15241 E AndroidRuntime: java.lang.SecurityException: Invalid component: ComponentInfo{com.android.settings/com.android.settings.Settings$IccLockSettingsActivity}
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1942)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1888)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.autofill.IAutoFillManager$Stub$Proxy.startSession(IAutoFillManager.java:413)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.autofill.AutofillManager.startSessionLocked(AutofillManager.java:836)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.autofill.AutofillManager.notifyViewEntered(AutofillManager.java:474)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.autofill.AutofillManager.notifyViewEntered(AutofillManager.java:453)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.View.notifyEnterOrExitForAutoFillIfNeeded(View.java:7022)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.View.onWindowFocusChanged(View.java:12141)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.widget.TextView.onWindowFocusChanged(TextView.java:9895)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.View.dispatchWindowFocusChanged(View.java:12106)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1416)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1416)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1416)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1416)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1416)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1416)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1416)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1416)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:4283)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:108)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.os.Looper.loop(Looper.java:206)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6735)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
05-02 14:58:57.154 15241 15241 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:845)
05-02 14:58:57.159  1020  1564 W ActivityManager:   Force finishing activity com.android.settings/.Settings$IccLockSettingsActivity

05-02 14:58:57.166  1020  1099 I ActivityManager: Showing crash dialog for package com.android.phone u0

問題原因

該 Issue 是某個 Google security patch 導致的。
在那個 Security patch 中,在進入和退出 EditText 時會進行 Auto fill 的 Security check。

可以採用 Disable auto fill 的方式來解決該問題,具體修改方法如下:

使用的 Settings 中的檔案"EditPinPreference.java"。

解決方法:

檔案路徑為:/packages/apps/Settings/src/com/android/settings/EditPinPreference.java


在方法"onBindDialogView()"中增加 Disable auto fill 的 Code,如下所示:

protected void onBindDialogView(View view) {
super.onBindDialogView(view);
final EditText editText = (EditText) view.findViewById(android.R.id.edit);
if (editText != null) {
editText.setInputType(InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_VARIATION_PASSWORD);
editText.setImportantForAutofill(
View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
}
}

檢視資料

https://www.jianshu.com/p/531ce99bd961

在Android 8.0中加入了Autofill framework,也就是自動填充框架,目的在於簡化了登入和信用卡表單之類表單的填寫工作。如果一個應用支援自動填充,系統從使用者的Google賬戶儲存的自動填充資訊中選取相應的內容來填充當前輸入框,比如下圖中,一個登入頁面的手機號碼輸入欄,自動填充的推薦內容則是當前使用者的手機號碼。

這個功能也不是什麼新的亮點,用過Chrome瀏覽器的人對自動填充功能肯定有所體驗,這次Google把它搬到了手機上,目的自然是想打通各個平臺。但是同時問題也來了,在某些頁面我並不是很想使用自動填充功能,因為填充的資料實在是不怎麼友好,比如對於一個簡單的搜尋框,系統給出的自動填充建議竟然是機主的姓名。再者就是有些敏感資料並不想儲存到自動填充的表單裡面,所以筆者根據參考官方文件和一些博文整理了本篇文章。

作為一個開發者,如何來控制自動填充功能呢?在Android O Developer Preview 3中google提供了一些解決辦法,即從sdk26開始,所有的檢視中新增了一個android:importantForAutofill的屬性,以及相應的一個方法setImportantForAutofill()來控制自動填充功能,具體的細節不細說,可參考官方文件,這裡只簡單說一下如何在程式碼裡控制自動填充功能。

  • 通過在 layout 檔案中給控制元件設定 android:importantForAutofill="no"或在程式碼中呼叫 ViewsetImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO) 方法。

  • 通過在 activity 的 onCreate() 方法中加入如下程式碼:

    getWindow()
    

.getDecorView()
.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
```
在這種情況下,這些控制元件的資料也不會提供給自動填充服務,不會被記錄。對於一個檢視容器來說,如果設定了noExcludeDescendants或者IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS,也就意味著不僅是該容器自身還是它的所有子檢視,都會受到相關影響。

對於某些安全性要求較高或者某些特殊的頁面,自動填充不再是一個優勢,反而帶來影響的時候,我們可以用上面的兩個方式來禁止自動填充功能。

對於某些系統版本,比如 Android O Preview 2中,以上兩個方式不會生效。這時候就可以用AutofillManager先檢查一下自動填充是否開啟,如果開啟了,採取迴避行動。

if (getSystemService(android.view.autofill.AutofillManager.class).isEnabled()) {
  Toast.makeText(this, "Ick!", Toast.LENGTH_LONG).show();
  finish();
}
可以參考這個;https://blog.csdn.net/arjinmc/article/details/73331022