Disabling Android O auto-fill service for an application

别说谁变了你拦得住时间么 提交于 2019-12-17 05:02:34

问题


Android O has the feature to support Auto-filling for fields. Is there any way I can disable it for a specific application. That is I want to force my application not to use the auto-fill service.

Is it possible ?

To block autofill for an entire activity, use this in onCreate() of the activity:

getWindow()
  .getDecorView()
  .setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);

Is there any better method than this ?


回答1:


Is it possible ?

Not that I am aware of. Certainly, nothing is documented.

Is there any better method than this ?

Not that I am aware of.




回答2:


Currently there is no direct way to disable the autofill for an entire application, since the autofill feature is View specific.

You can still try this way and call BaseActivity everywhere.

public class BaseActivity extends AppCompatActivity {

    @Override
    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);
    }
}

You can also force request autofill this way.

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

Note: At the moment autofill feature is only available in API 26 Android Oreo 8.0

Hope this helps!




回答3:


I believe the accepted answer is incorrect:

So I have my own class which is extends the android.support.v7.widget.AppCompatEditText and all I did is overwrote the following method with the following value:

@Override
public int getAutofillType() {
    return AUTOFILL_TYPE_NONE;
}

no other solutions worked, not even android:importantForAutofill="no".

getAutofillType() comes from the View class, so it should work for every other class such as TextInputEditText too!




回答4:


I ran into this too. It turns out the issue was caused by setting the hint text on the EditText nested inside the TextInputLayout.

I did some digging and found this nugget in the 26.0.0 Beta 2 release notes. Andorid Support Release Notes June 2017

TextInputLayout must set hints on onProvideAutofillStructure()

That led me to try setting the hint on the TextInputLayout instead of the nested EditText.

This resolved the crashing issue for me. Example:

            <android.support.design.widget.TextInputLayout
                android:id="@+id/textInputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Some Hint Text"
                android.support.design:hintAnimationEnabled="true"
                android.support.design:hintEnabled="true"
                android.support.design:layout_marginTop="16dp">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

            </android.support.design.widget.TextInputLayout>



回答5:


Seems to be a bug that needs to be fixed : https://issuetracker.google.com/issues/67675432
In the meanwhile a workaround for now is to disable the AutoFill feature for the whole project. You can add in the values-v26/styles.xml file the following style or you can edit your BaseEditTextStyle if you are using a specific style for your EditText views.

<style name="App_EditTextStyle" parent="@android:style/Widget.EditText">
<item name="android:importantForAutofill">noExcludeDescendants</item>
</style>

and in the values-v26/themes.xml file you can simply add to the default theme that you are using in your app the items editTextStyle and android:editTextStyle like following :

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:editTextStyle">@style/App_EditTextStyle</item>
<item name="editTextStyle">@style/App_EditTextStyle</item>
</style>

this way you can apply this changes for all your EditTexts without needing to change your layout files or Activities (and later on you can easily remove it when the bug is fixed).




回答6:


In your EditText attributes add android:importantForAutofill="no" This should be a temporary fix and will only apply to api 26+




回答7:


Create custom EditText style and set android:importantForAutofill to no.

<style name="EditTextStyleWithoutAutoFill" parent="Widget.AppCompat.EditText">
    <item name="android:importantForAutofill">no</item>
</style>

Then in your activity theme set this style for editTextStyle.

<item name="android:editTextStyle">@style/EditTextStyleWithoutAutoFill</item>



回答8:


With reference to Google issue tracker, it has been fixed.

This is fixed on Android 8.1

If any issue persists, please report at Google issue tracker they will re-open to examine.



来源:https://stackoverflow.com/questions/45731372/disabling-android-o-auto-fill-service-for-an-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!