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
    #!/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,
     


  2. scary alien

    scary alien not really so scary
    Moderator

Loading...
Similar Threads - Method toggling screen
  1. JohnJSal
    Replies:
    9
    Views:
    468
  2. consultant
    Replies:
    0
    Views:
    190
  3. Rob Sleurs
    Replies:
    0
    Views:
    326
  4. LAMPROS MARANTOS
    Replies:
    0
    Views:
    498
  5. Ravindra Pal Singh
    Replies:
    0
    Views:
    652
  6. Randy_Gold
    Replies:
    1
    Views:
    1,000
  7. Randy_Gold
    Replies:
    1
    Views:
    577
  8. Alexenferman
    Replies:
    0
    Views:
    658
  9. umtblbl
    Replies:
    0
    Views:
    451

Share This Page

Loading...