Overview

Skill Level: Intermediate

Android, Kotlin, Java

Android prevent screen off is the library that keeps the screen on until user is looking at the screen. If you are using Samsung devices you might know there is one feature called “Smart Stay”.

Ingredients

“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.

Read this also : Create Augmented Reality in Kotlin Using ARCore SDK

Step-by-step

  1. What is this library for?

    • Ideally, when you user is looking at the screen, your application should not turn the screen off. This is huge deal for the blogging, messaging applications because those applications displays textual content to the user. Reading those textual content takes more time to the user. While reading that content (let say anu article) if the screen turns off, because of the screen timeout that is frustrating to the user.
    • This library provides smart handling of the screen on-off. This library prevents screen from turning off if your user is looking at the screen might reading some textual content on the screen. As soon as the user stop looking at the screen it will allow phone screen to turn off.

    Do you know : How Android Image Loading Library Works?

  2. How this library works?

    1. This library uses the front camera to sense when you are looking at your device
    2. This uses Google Play Services Mobile Vision API to track users eye using the device front camera.
    3. It keeps the screen from turning off regardless of the screen timeout setting if the user is looking at the screen by detecting user’s eyes.
  3. Integrating this in your application:

    Source : What and How implement Android Prevent Screen 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.

    dependency{
    compile 'com.kevalpatel2106:prevent-screen-off:1.0'
    }

     

    Initialize in your activity:

    • First, you need to inherit AnalyserActivity in 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.
    • Library will start tracking user eyes as soon as activity comes into the foreground. If the user is looking at the screen this will prevent the screen from turning off.
    • When library detects that the user is not looking at the screen, this will turn off the screen after some time.
    • It will stop eye tracking when your activity goes into background to preserve the battery.
    • Implement ScreenListener to receive the callbacks from the library.
    public class MainActivity extends AnalyserActivity //Inherit AnalyseActivity to automatically manage activity callback.
    implements ScreenListner { //Implement the listener to get the callbacks
    • Handle the callbacks and errors received from ScreenListener.
    public void onScreenMonitoringStart() {
    //This callback will receive when eye tracking algorithm is intilized.
    }
    @Override
    public void onScreenMonitoringStop() {
    //This callback will receive when eye tracking algorithm is stopped.
    }
    @Override
    public void onErrorOccurred(int errorCode) {
    switch (errorCode) {
    case Errors.UNDEFINED:
    //Error is not defined.
    //Library won't control the screen on/off anymore.
    break;
    case Errors.CAMERA_PERMISSION_NOT_AVAILABLE:
    //Camera permission is not available ask for the runtime camera permission
    break;
    case Errors.FRONT_CAMERA_NOT_AVAILABLE:
    //Device does not have the front camera.
    //So, this library won't control the screen on/off.
    break;
    case Errors.LOW_LIGHT:
    //Low light in the surrounding environment so that eye tracking cannot work.
    //Library won't control the screen on/off anymore.
    break;
    case Errors.PLAY_SERVICE_NOT_AVAILABLE:
    //This device doesn't have the play services installed.
    // The SDK will display the error dialog it self. This will stop the eye tracker and will not
    // prevent screen off automatically.
    break;
    }
    }
    • That’s it. You successfully integrated the library.

    Recommended : Android Oreo Vs iOS 11 : Comparison you should know about this

  4. Where can you use this feature?

    • Tracking user’s eye using device camera consumes more battery. So, it is advisable that you don’t integrate automatic screen control in every screen of your application.
    • You can integrate this features in your application activity, which has more textual content to read. An e.g. activity that shows chat conversation in messaging app or activity that displays full article in your blogging application. (This list can be extended for may other use cases. Let me know if you have more ideas in comments.)

Join The Discussion