Class WakeLockApi

java.lang.Object
com.oorian.html.js.JavaScriptApi
com.oorian.html.js.wakelock.WakeLockApi

public class WakeLockApi extends JavaScriptApi
Provides access to the Screen Wake Lock API from server-side Java code.

The Screen Wake Lock API prevents the device from dimming or locking the screen. This is useful for applications that need to keep the screen on, such as:

  • Video players
  • Navigation apps
  • Presentation apps
  • Recipe apps (cooking mode)

Note: The wake lock is automatically released when the page becomes hidden (e.g., user switches tabs). You may want to re-acquire it when the page becomes visible again.

Usage:


 public class MyPage extends HtmlPage implements WakeLockListener {

     @Override
     protected void createBody(Body body) {
         body.registerListener(this, WakeLockEvent.class);
         body.registerListener(this, WakeLockErrorEvent.class);

         // Acquire wake lock to keep screen on
         WakeLockApi.request(body);
     }

     @Override
     public void onEvent(WakeLockEvent event) {
         if (event.isAcquired()) {
             showMessage("Screen will stay on");
         } else {
             showMessage("Wake lock released");
         }
     }

     @Override
     public void onEvent(WakeLockErrorEvent event) {
         showMessage("Wake lock failed: " + event.getMessage());
     }
 }
 
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • WakeLockApi

      public WakeLockApi()
  • Method Details

    • request

      public static void request(Element element)
      Requests a screen wake lock using the current page context.

      Once acquired, the screen will not dim or lock until the wake lock is released or the page becomes hidden.

      Parameters:
      element - The element that will receive wake lock events.
    • request

      public static void request(HtmlPage page, Element element)
      Requests a screen wake lock.

      Once acquired, the screen will not dim or lock until the wake lock is released or the page becomes hidden.

      Parameters:
      page - The page context.
      element - The element that will receive wake lock events.
    • release

      public static void release(Element element)
      Releases the current wake lock using the current page context.
      Parameters:
      element - The element that will receive the release event.
    • release

      public static void release(HtmlPage page, Element element)
      Releases the current wake lock.
      Parameters:
      page - The page context.
      element - The element that will receive the release event.
    • checkSupport

      public static void checkSupport()
      Checks if the Screen Wake Lock API is supported by the browser using the current page context.

      Note: This executes JavaScript that logs to console.

    • checkSupport

      public static void checkSupport(HtmlPage page)
      Checks if the Screen Wake Lock API is supported by the browser.

      Note: This executes JavaScript that logs to console.

      Parameters:
      page - The page context.