Wednesday, January 13, 2016

Android - Keeping the screen on

Multiple approaches were discussed  in the forum thread. Below were tried and few results on that. 

PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "no sleep");
wakeLock.acquire();

Below permission had to be requested in the Manifest file.

"android.permission.WAKE_LOCK" />

In the service on Destroy, below code needs to be added as well. 

wakeLock.release();

However, this actually did not work, screen was locked even though this was specified. Reading more on the API Documentation, apart from the PARTIAL_WAKE_LOCK, it also had FULL_WAKE_LOCK or DIM but these are now deprecated

The API documentation suggests something like below 

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
The above cannot be called from Service, instead needs to be called from Activity or Fragment. 

By calling the above from an activity, below was my experience on Android lollipop device. 

1. With the activity that called the API on the screen, the screen never locked up 
2. With the activity that called the API pushed to background, but with an overlay window on the screen, the screen gets locked up
3. With the app killed, definitely the screen gets locked up

References:

No comments:

Post a Comment