1. Are you ready for the Galaxy S20? Here is everything we know so far!

Method of toggling screen on/off programmatically?

Discussion in 'Android Development' started by Brachdev, Feb 20, 2016.

  1. Brachdev

    Brachdev Lurker
    Thread Starter

    I’m looking for any suggestions on a good method to toggle a tablet’s screen on and off programmatically.

    I’m working with a number of physically mounted tablets in a non-commercial setting. They are all connected continuously to AC power, rooted, and all are set to “stay wake, screen will never sleep while plugged in.” Half run Jelly Bean, the other Marshmallow.

    On each tablet I have created/deployed a rudimentary listener using DataInputStreams.readUTF to receive commands send remotely via NetCat. Essentially the remote program instructs the tablets when to display/refresh specific webpages and to turn the screens on and off.

    Disclaimer: I just started dabbling in Java/Android programming a few days ago. The only solution I could get to work is really crappy and sad. Currently my app calls a shell script that executes an input keyevent command. Below is an example of the ‘screen-on’ code. I use a similar method for ‘screen-off.’

    # SCRIPT
    root@otter:/sdcard/scripts # cat screenon.sh
    ScrnOn=`dumpsys input_method | grep -c mScreenOn=true`
    if [[ $ScrnOn -eq 0 ]]; then
    input keyevent 26

    # APP CODE
    public void turnOnScreen(){
    Process su = Runtime.getRuntime().exec("su");
    DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
    }catch(IOException e){
    }catch(InterruptedException e){

    There must be a better way to do this directly in the app code. Any suggestions would be appreciated.

    1. Download the Forums for Android™ app!


  2. scary alien

    scary alien not really so scary
    VIP Member


Share This Page