keyboard not showing when phone is locked and an activity disables keyguard
Asked Answered
M

1

5

I ran into an issue with Android 6.0. Marshmallow was loaded to a Nexus 6 from the official Nexus factory firmware image site (https://developers.google.com/android/nexus/images).

I have an app where I use AlarmManager to start an activity that disables keyguard so the activity can be visible even if the phone is locked (like an alarm clock app does). In this activity if the user clicks on a button then it opens a dialog where the user should type in an EditText view. When the dialog shows or the user clicks on the EditText it should open the keyboard. This has been working until now and seems to be working on every OS version except 6.0 on my Nexus 6.

I suspect that the reason is that the phone is locked when the Activity starts as if I start the Activity when the phone is unlocked then the keyboard shows perfectly. This seems to only happen on 6.0.

Can anyone confirm this or let me know if something has changed in 6.0 that I'm not aware of?

Thanks.

Milagrosmilam answered 14/10, 2015 at 16:52 Comment(0)
E
7

Finally I found a working solution. Dialog windows seem to have their own flags in Marshmallow which have to bet set. I did it in AlertDialog like this:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("My Title");
// Add other stuff for AlertDialog here
AlertDialog alertDialog = builder.create();
alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
alertDialog.show();

For my PreferenceActivity I had to extend the EditTextPreference in order to solve the issue and then use myappname.TextPref instead of EditTextPreference in the XML config file.

class TextPref extends EditTextPreference {

    public TextPref(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void showDialog(Bundle state) {
        super.showDialog(state);

        Dialog dialog = getDialog();
        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}
Ell answered 29/3, 2016 at 20:35 Comment(3)
Works perfectly. Did I miss this somewhere in the docs or this change is completely undocumented?Milagrosmilam
I didn't find anything in the docs and just tried around. Unfortunately there seems to be the same issue with Toast messages. However I don't have a workaround as there seems to be no way to get the Window of a Toast.Ell
Maybe you can try to use SnackBar instead of Toast. Would that work for you?Milagrosmilam

© 2022 - 2024 — McMap. All rights reserved.