Did you know that you can navigate the posts by swiping left and right?
Ideally, when you user is looking at the screen, your application should prevent the device screen from turning off.
This is the huge deal for the blogging, messaging applications. These applications display textual content to their user. Reading those textual content takes more time to the user. While reading that content (let say an article) if the user does not interact with the display and the screen turns off because of the screen timeout that is frustrating to the user. It will destroy the concentration of the user and breaks the link.
If you are using Samsung devices you might know there is one feature called “Smart Stay”. When this feature is enabled, the device will prevent your screen from turning off regardless of your screen timeout settings. But, this technology is only available on some Samsung devices. What if we want to use that technology in our application to solve above problem? Today, I am going to present the solution of this problem.
Prevent-Screen-Off library handles screen on/off timing smartly. It prevents device display from turning off when the user is looking at the screen and he/she might be reading some textual content provided by your application. As soon as the user stop looking at the screen it will allow phone screen to turn off.
Let’s dive into some technical stuff and see how you can integrate this library in your application and use it.
Add Gradle dependency:
To start with the integration, first provide the Gradle dependency of the library by entering below lines in your module level build.gradle.
This library adds android.permission.CAMERA and android.permission.WAKE_LOCK permission in your applications AndroidManifest.xml file.
Initialize in your activity:
AnalyserActivityin the activity which you want to control screen on/off automatically. The library will synchronize with your activity life-cycle and start and stop eye tracking based on your activity state.
ScreenListener to receive the callbacks from the library.
Handle the callbacks and errors received from