How to Manage Screen On/Off Functionality

From wizarPOS
Revision as of 22:35, 28 December 2023 by Simon (talk | contribs)

Overview

This guide provides a code snippet to demonstrate how to manage screen on/off functionality.

Code Snippet

The specific code snippet for this function will be detailed here, showcasing how to programmatically turn the screen on or off.

private void goToLockNow() {
        try {
            boolean result = systemExtApi.setDeviceOwner(this.getPackageName(),LockReceiver.class.getName());
            if(result){
                DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
                devicePolicyManager.lockNow();
                SystemClock.sleep(2000);
                mWakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
                        | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName());
                wakeUp();
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * Wake up screen
     */
    private void wakeUp() {
        mWakeLock.acquire();
        if (mWakeLock.isHeld()) {
            mWakeLock.release();
        }
    }

Download

Demo download