Forum Xamarin.Android

Stop android device from hibernation

Hi there
I'm going to use the Android device as a punch clock which is going to be powered permanently.
How to stop the device from hibernating programmatically to make it work unattended for weeks?


  • DanielVartdalDanielVartdal USMember ✭✭


    have you looked at WakeLock?

    "A wake lock is a mechanism to indicate that your application needs to have the device stay on."

    "Any application using a WakeLock must request the android.permission.WAKE_LOCK permission in an element of the application's manifest. Obtain a wake lock by calling newWakeLock(int, String)."

    "Call acquire() to acquire the wake lock and force the device to stay on at the level that was requested when the wake lock was created."

    "Call release() when you are done and don't need the lock anymore. It is very important to do this as soon as possible to avoid running down the device's battery excessively."

  • CheesebaronCheesebaron DKInsider, University mod
  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    Wouldn't it be good to go with Window.AddFlags(WindowManagerFlags.KeepScreenOn); depending on what he really needs?

  • DanielVartdalDanielVartdal USMember ✭✭

    Yes, that one should also work if @NickRenzhiglov only needs to keep the screen on per activity.

    He could also use:

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android=""
            android:keepScreenOn="true" />

    in his activity-view.

    It all depends on what he needs. If wake lock is present, and he somehow gets out of the activity (punches the wrong button? ex: home menu, another button on the app) the wake lock will not be released.

    If the same happens when using the keepScreenOn flag, the flag will be released when leaving the activity.

    So, using the keepScreenOn either programatically or code behind will work, and the device will not go to hibernation as long as he always stays in the activity where the keepScreenOn flag is set.

    For the simplest possible use(and less code) set the flag as @sebastianseidel mentioned.
    In addition to less code, you don't need to ask for the android.permission.WAKE_LOCK permission in your manifest.

Sign In or Register to comment.