Utsav Gupta
Utsav Gupta

Reputation: 3971

How to set screen off on a rooted android device?

I have a rooted android device . I wish to set display off on it at a scheduled time.

How can i set screen off programatically on that? Can we do it using Linux commands?

Thanks.

Upvotes: 3

Views: 3697

Answers (3)

wuseman
wuseman

Reputation: 1676

sleeping="$(adb shell dumpsys power | grep 'mWakefulness=')"
screen="$(adb shell dumpsys nfc| grep 'mScreenState=')"

case "$screen" in
  "mScreenState=OFF")
     echo "* Device is not unlocked."
                 ;;
  "mScreenState=ON_LOCKED")
     echo "* Device is not unlocked."
     sleep 0
     exit ;;
   "mScreenState=ON_UNLOCKED")
       echo "* Locking screen.."
       adb shell input keyevent 26
       echo "* Screen turned off"
       exit
esac

echo -e "$basename$0: internal error -- can't lock screen since it's not unlocked"

Upvotes: 0

Kadir Aydin
Kadir Aydin

Reputation: 1

For those with the same issue like me: I found on my MTK device following code to turn off the screen, because keyevent 26 doesn't work on my device:

adb shell input keyevent 6

will turn off the devices screen also. To turn on the devices screen, you can do it with keyevent 26.

The best way to find out which keyevent works for your device is with a tiny bash script like this:

#!/bin/bash
while
do
    count=$(( $count+1 ))
    input keyevent $count
    echo "input keyevent $count"
    sleep 1
done

Upvotes: 0

Saurabh Meshram
Saurabh Meshram

Reputation: 8416

You can use, adb shell input keyevent

Usage :

adb shell input keyevent [--longpress] <key code number or name>

To Turn your Screen off, use Keycode

26 -->  "KEYCODE_POWER"`

Run this from your prompt:

root@user:/$ adb shell input keyevent 26   

Or this,

root@user:/$ adb shell input keyevent KEYCODE_POWER

Run the above command from your Function/Method : Execute ADB Command through Android Application.

This procedure works for a Non-rooted device too.

Complete List of Events : See Here
Setting up ADB : See Here

Upvotes: 7

Related Questions