DEV Community

HarmonyOS
HarmonyOS

Posted on

How to prevent automatically locks screen

Read the original article:How to prevent automatically locks screen

Problem Description

The application automatically locks the screen when it is on a page that needs to be always on. For example, the clock application in the figure below needs to remain always on when the application is opened so that the user can check the time.

image.png

Background Knowledge

Page brightness setting supports developers to set page brightness. The window is the medium for brightness setting. The change of the window will cause the brightness of all pages under the window to change accordingly, so a mechanism for recording pages and monitoring page changes is needed to dynamically set the brightness of different pages.

Monitoring mechanism: uiObserver.on monitors the status changes of the NavDestination component.
Brightness setting: setWindowBrightness sets the screen brightness value of the application main window
Screen always on: setWindowKeepScreenOn sets whether the screen is always on.

Troubleshooting Process

Search the keyword SetKeepScreenOn in the log to check whether the application sets the screen to always be on.

kbs--f5142eaeee2a4998978d88f53f5bbb7f-ab14e.png

When the screen is set to stay on, there will be a try lock log output, and when the screen is unlocked, there will be a try unlock log output, and only type: 0 is concerned. Use the RunningLock filter log to view the lock and unlock time and confirm the timing of lock and unlock. As shown below, the screen is unlocked immediately after the screen is set to stay on.

kbs--fed5067eebd0461d83fb2c13d94dca34-1b1ab.png

Analysis Conclusion

The app does not have the screen always on.
The timing of locking and unlocking is inappropriate.

Solution

In order to keep the application on the page that needs to be always on, first use any method among getLastWindow() , createWindow() , and findWindow() to obtain the Window instance (windowClass). Take getLastWindow() as an example to obtain windowClass:

// EntryAbility.ets
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';


export default class EntryAbility extends UIAbility {
  // ...
  onWindowStageCreate(windowStage: window.WindowStage) {
    console.info('onWindowStageCreate');
    let windowClass: window.Window | undefined = undefined;
    try {
      window.getLastWindow(this.context, (err: BusinessError, data) => {
        const errCode: number = err.code;
        if (errCode) {
          console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`);
          return;
        }
        windowClass = data;
        console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
      });
    } catch (exception) {
      console.error(`Failed to obtain the top window. Cause code: ${exception.code}, message: ${exception.message}`);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Use setWindowKeepScreenOn to set the screen to always on.

import { BusinessError } from '@kit.BasicServicesKit';


let isKeepScreenOn: boolean = true;
try {
  windowClass.setWindowKeepScreenOn(isKeepScreenOn, (err: BusinessError) => {
    const errCode: number = err.code;
    if (errCode) {
      console.error(`Failed to set the screen to be always on. Cause code: ${err.code}, message: ${err.message}`);
      return;
    }
    console.info('Succeeded in setting the screen to be always on.');
  });
} catch (exception) {
  console.error(`Failed to set the screen to be always on. Cause code: ${exception.code}, message: ${exception.message}`);
}
Enter fullscreen mode Exit fullscreen mode

Written by Ahmet Furkan Sevim

Top comments (0)