Disabling Android O auto-fill service for an appli

2020-01-24 21:33发布

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 ?

10条回答
乱世女痞
2楼-- · 2020-01-24 21:45

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.

查看更多
仙女界的扛把子
3楼-- · 2020-01-24 21:54

In my case, our app targets sdk version 21 but newer devices (26+) were still popping up the autocomplete. Pretty big problem if the app runs on devices that are shared between people. Using just android:importantForAutofill="no" did not work for me.

The only solution that I found to work in my case was:

<EditText
    android:importantForAutofill="no"
    tools:targetApi="o"
    android:autofillHints="AUTOFILL_HINT_SMS_OTP" ...

The reason I added android:autofillHints="AUTOFILL_HINT_SMS_OTP" was because if you long pressed on the EditText it would still bring up autofill. Basically I told the field's autofill that it is waiting for a text message that will never be sent. Bit of a hack, I know...

Note: you may have to add xmlns:tools="http://schemas.android.com/tools" to your schemas' if it is not there already.

查看更多
唯我独甜
4楼-- · 2020-01-24 21:59

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楼-- · 2020-01-24 22:01

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!

查看更多
我只想做你的唯一
6楼-- · 2020-01-24 22:01

Had the same problem with API 28+ and disable Autofill. For me the only solution was to disable long click for my views.

                <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:longClickable="false"
                    android:text="@={model.emailAdress}"/>
查看更多
闹够了就滚
7楼-- · 2020-01-24 22:02

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

查看更多
登录 后发表回答