0

I am responsible for maintaining an Android application developed in Java. The client has requested a new functionality:

  • That the screen does not turn off, as Google maps does on the driving screen.

The development is done for a specific device with Android 9 and does not have any security mechanism configured to unlock the screen. Looking for the solution I have found that you have to use the methods setShowWhenLocked(boolean showWhenLocked) and setTurnScreenOn(boolean turnScreenOn) in the activities. I have created the following method:

public class ActivityUtils
{
    public static void dontLockScreen(Activity activity)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1)
        {
            activity.setShowWhenLocked(true);
            activity.setTurnScreenOn(true);
            KeyguardManager keyguardManager = (KeyguardManager)activity.getSystemService(Context.KEYGUARD_SERVICE);
            keyguardManager.requestDismissKeyguard(activity, null);
        }
    }
}

This code does not do what the client wants, since the screen turns off one minute after the last interaction (as configured in settings). In settings you can configure that the screen is locked, but what you want is to ignore this setting as navigation applications or video players do.

Do you know what is the correct way to achieve this functionality?

Jon
  • 891
  • 13
  • 32

0 Answers0