1. Download our Official Android App: Forums for Android!

Apps Method of toggling screen on/off programmatically?

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

  1. Brachdev

    Brachdev Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    1
    Joined:
    Feb 19, 2016

    Feb 19, 2016
    1
    0
    15
    Male
    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
    #!/system/xbin/bash
    ScrnOn=`dumpsys input_method | grep -c mScreenOn=true`
    if [[ $ScrnOn -eq 0 ]]; then
    input keyevent 26
    fi


    # APP CODE
    public void turnOnScreen(){
    try{
    Process su = Runtime.getRuntime().exec("su");
    DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
    outputStream.writeBytes("/sdcard/scripts/screenon.sh\n");
    outputStream.flush();
    outputStream.writeBytes("exit\n");
    outputStream.flush();
    su.waitFor();
    }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.
    Thanks,
     

    Advertisement

  2. scary alien

    scary alien not really so scary
    Moderator
    Rank:
     #9
    Points:
    2,138
    Posts:
    22,319
    Joined:
    Mar 5, 2010

    Mar 5, 2010
    22,319
    23,814
    2,138
    Male
    space alien ;)
    Indy

Share This Page

Loading...