Is it possible to adjust the screen dimmer and the lock screen on Android?

Kim Strasser 1,321 Reputation points
2024-08-18T09:35:22.1333333+00:00

Is it possible to adjust the amount of time until the screen dimmer starts on Android phones/tablets while my game is running?

Is it possible to adjust the amount of time until the lock screen on Android is activated while my game is running? Is it allowed to deactivate the lock screen when my game is running so that the lock screen is not automatically executed after some time?

Developer technologies | .NET | .NET MAUI
Developer technologies | Visual Studio | Other
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-08-19T07:31:23.7766667+00:00

    Hello,

    Is it possible to adjust the screen dimmer and the lock screen on Android?

    Yes, you can open your Activity1.cs, then override the OnAttachedToWindow method. Then create a method, set the KEEP_SCREEN_ON flag and DismissKeyguard flag in WIndow. And set TurnScreenOn and ShowWhenLocked to true. I test it in my devices, it will not lock the screen when application running.

    You can refer to the following code.

    public override void OnAttachedToWindow()
    {
         base.OnAttachedToWindow();
         DisabletheLockScreen();
    }
     
    private void DisabletheLockScreen()
    {
    
     
         Window.AddFlags(WindowManagerFlags.DismissKeyguard |WindowManagerFlags.KeepScreenOn);
       if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
         {
             SetTurnScreenOn(true);
     
             SetShowWhenLocked(true);
        }
         else
         {
             Window.AddFlags(WindowManagerFlags.ShowWhenLocked | WindowManagerFlags.KeepScreenOn);
         }
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Vinay Shokeen 0 Reputation points
    2024-08-20T09:12:58.28+00:00

    Yes, it is possible to adjust the amount of time until the screen dimmer starts on Android phones/tablets while your game is running. You can do this by adjusting the display settings in your device’s system settings. The exact steps may vary depending on the version of Android you are using and any customizations made by manufacturers, but generally, you should be able to find these options under Settings > Display or Settings > Screen.

    As for adjusting the lock screen activation time or deactivating it entirely while your game is running, this functionality depends on whether such features are supported by your specific device and its operating system version. Some devices allow users to customize lock screen timeout duration through their security settings (Settings > Security), where they can set a longer delay before activating or even disable automatic locking altogether during certain activities like gaming.

    However, keep in mind that modifying lock screen behavior might have implications for security and privacy as disabling lockscreens completely could leave sensitive information accessible without authentication if someone gains physical access to your device.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.