How to Manage Screen On/Off Functionality: Difference between revisions
(Created page with "== Screen on/off == Here is the snippet code: <syntaxhighlight lang="java"> private void goToLockNow() { try { boolean result = systemExtApi.setDeviceOwner...") |
No edit summary |
||
Line 1: | Line 1: | ||
== | == 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. | |||
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> | ||
private void goToLockNow() { | private void goToLockNow() { |
Revision as of 22:35, 28 December 2023
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();
}
}