I wanted to rotate the screen orientations i.e from landscape -> patriot and vice verse for every 500 ms(This is on real device(not on emulator)).
Is there any shell command where we can rotate the current screen orientation? This is not corresponding to any of the app. I just want to rotate the screen in all available directions with irrespective to current activity
I've checked with adb shell to change the screen to landscape:
service call window 18 i32 1
change the screen to portrait:
service call window 18 i32 0
But these are not working on real device.. Can any one please provide a better solution to do this, would really helpful for me.
Atleast share/point me to any available scripts/apks that will do this auto orientations.
Thanks in advance
Well adb shell is still a shell (if a bit cut down)
You can do this:
sw=1;
while true; do
[[ "$sw" = 1 ]] && sw=0 || sw=1;
service call window 18 i32 $sw;
sleep 1
done;
And this is the one line version of it (in case you can not write it in file)
sw=1; while true; do [[ "$sw" = 1 ]] && sw=0 || sw=1; service call window 18 i32 $sw; sleep 1; done;
This will switch orientation every second (I don't think sleep works with less than seconds)
I came across the same problem, and found a solution in here.
briefly, that's what you have to do:
First disable the auto rotation by using the following command:
adb shell content insert --uri content://settings/system --bind name:s:accelerometer_rotation --bind value:i:0
and to rotate:
adb shell content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:x
for landscape replace x (in the end of the line) with 1 and for portrait with 0, and the code from the previous comment should work just fine :)
Related
We can able to measure the launching time of any app using "adb shell am start -w -n yourpackagename/.activityname" (Wait for launch to complete). This adb shell command will print TotalTime & ThisTime.
But I have following questions?
What is the difference between TotalTime & ThisTime?
What is the right metric to measure the performance ? ThisTime or TotalTime ?
Why sometimes both ThisTime & TotalTime values are equal? why sometimes both are different ?
Example:
adb shell am start -W -n com.android.settings/.ApplicationSettings
ThisTime: 1554
TotalTime: 42815
Please, look at the answer of this question
"thisTime": just current activity launched time
"totalTime":the activity you started may be on the bottom of activity stack. So it refers to the total time from activity searching
to current activity launched.
I am working on a kiosk project using an android tablet.If there is no input power to the kiosk for a long time then, the tablet will eventually shutdown.In order to auto-reboot the tab when the power comes back I modified the code in my tabs battery animation file(ipod) using:
#!/system/bin/sh
sleep 300
/system/bin/reboot
However,during the sleep period(shown above),the tabs screen remains in the ON state (and hence takes longer retries for the tab to charge and bootup).I need to turn the screen OFF.
What is the command I should use prior to the sleep command to turn my tabs screen OFF during the charging/bootup stage. Thanks !
Try this command too,
echo 100 > /sys/devices/platform/nov_cabc.0/leds/lcd-backlight/brightness
or
echo 100 > brightness
I'm using Android 4.4 on a real device and I want to set the device orientation via adb. I don't want it done with uiautomator since it won't last after the termination of the uiautomator code.
How can I do this?
Instead of using "adb shell content", there's a more clean way by using "adb shell settings". They are doing the same thing, put value to settings provider.
adb shell settings put system accelerometer_rotation 0 #disable auto-rotate
adb shell settings put system user_rotation 3 #270° clockwise
accelerometer_rotation: auto-rotation, 0 disable, 1 enable
user_rotation: actual rotation, clockwise, 0 0°, 1 90°, 2 180°, 3 270°
You may first need to turn off the automatic rotation:
adb shell content insert --uri content://settings/system --bind name:s:accelerometer_rotation --bind value:i:0
Rotate to landscape:
adb shell content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:1
Rotate portrait:
adb shell content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:0
Disable accelerometer_rotation and set the user_rotation
user_rotation Values:
0 # Protrait
1 # Landscape
2 # Protrait Reversed
3 # Landscape Reversed
accelerometer_rotation Values:
0 # Stay in the current rotation
1 # Rotate the content of the screen
Example using adb:
adb shell settings put system accelerometer_rotation 0
adb shell settings put system user_rotation 3
Example programmatically:
import android.provider.Settings;
// You can get ContentResolver from the Context
Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
Settings.System.putInt(getContentResolver(), Settings.System.USER_ROTATION, 3);
wm cmd can be used to set the user rotation on adb shell
wm help
set-user-rotation [free|lock] [-d DISPLAY_ID] [rotation]
Set user rotation mode and user rotation.
Example:
wm set-user-rotation lock 0
wm set-user-rotation lock 1
I found some info on the web:
echo 1 /sys/devices/platform/flashlight.0/leds/flashlight/brightness
But on my Nexus 7 (flashed an AOSP), I couldn't find that directory.
Any idea about which file should I write to? Is this doable?
After ls /sys/devices/platform, I got:
LID
alarm
arm-pmu.0
bcm4330_rfkill
bcmdhd_wlan.1
bluesleep
fiq_debugger.0
fsl-tegra-udc
gpio-keys.0
grouper_misc
leds-gpio
oprofile-perf.0
power
power.0
pwm-backlight
ram_console
reg-dummy
reg-fixed-voltage.1
reg-fixed-voltage.10
reg-fixed-voltage.11
reg-fixed-voltage.2
reg-fixed-voltage.3
reg-fixed-voltage.4
reg-fixed-voltage.6
reg-fixed-voltage.8
regulatory.0
sdhci-tegra.2
sdhci-tegra.3
serial8250
snd-soc-dummy
spdif-dit.0
spdif-dit.1
spi_tegra.0
spi_tegra.3
tegra-ehci.1
tegra-i2c.0
tegra-i2c.1
tegra-i2c.2
tegra-i2c.3
tegra-i2c.4
tegra-nvmap
tegra-otg
tegra-pcm-audio
tegra-se
tegra-snd-rt5640.0
tegra30-ahub
tegra30-dam.0
tegra30-dam.1
tegra30-dam.2
tegra30-hda
tegra30-i2s.1
tegra30-i2s.3
tegra30-i2s.4
tegra30-spdif
tegra_camera
tegra_pwm.0
tegra_rtc
tegra_smmu
tegra_uart.1
tegra_uart.2
tegra_uart.3
tegra_uart.4
tegra_wdt
uevent
There is a new binary shipping with Android Jellybean 4.2, which can be used to directly read/write to the system settings provider, accessible via command line.
For example: in order to increase brightness of the screen, use below command:
adb shell settings put system screen_brightness 200
Read more about SCREEN_BRIGHTNESS Note that the range of values is [0 - 255]
The range of values is not necessarily from 0 - 255. On my OnePlus for example
it ranges from 0-2047. If you want to know yours just set the brightness slider to max and
then type: adb shell settings get system screen_brightness.
You should use the pwm-backlight!
You can use this adb command to set screen brightness
adb shell settings put system screen_brightness 255
and if your device has auto brightness setting then use this command first
adb shell settings put system screen_brightness_mode 0
This did not work for the latest Nexus 7 (2013) for me. But this answer explains the way to find what you are looking for on any version: https://stackoverflow.com/a/13492336
However we need to note that in order to change any of this you need to have root access.
Is there a way to lock the Android screen via the ADB?
I find ways to lock the display in an apk, but I want to lock the screen from the PC via ADB, to simulate a display timeout, without having to wait for the timeout.
Is it possible to do this?
Thanks,
Diane
Cool, I just found KEYCODE_POWER which is 26.
so it works by sending:
adb shell input keyevent 26
which locks the screen if the screen is unlocked. If the screen is already locked, it wakes up the device.
My guess is that the only way to ensure that the screen is locked (off), is to unlock (we use keyevent 82 (menu), then lock it with the power button keyevent. Does anyone have any idea if this is true?
Michael R. Hines gave the what is arguably the easiest solution. However, the following line is not useful in later versions of Android.
adb shell input keyevent 82 # unlock
I've updated the shell script using coordinates for the individual device I want to wake (Tablet). My tablet does not support orientation changes for lockscreen events, so the values always work as the lockscreen is always in landscape. Should you require orientation change detection, a simple if/then/else would suffice in picking the correct coordinates to use for the orientation.
#!/bin/bash
if [ "$(adb shell dumpsys power | grep mScreenOn= | grep -oE '(true|false)')" == false ] ; then
echo "Screen is off. Turning on."
adb shell input keyevent 26 # wakeup
adb shell input touchscreen swipe 930 380 1080 380 # unlock
echo "OK, should be on now."
else
echo "Screen is already on."
echo "Turning off."
adb shell input keyevent 26 # sleep
fi
Here's the whole thing in one single bash script which checks if the screen is actually on or not and then wakes up and unlocks the screen in one shot:
if [ "$(adb shell dumpsys power | grep mScreenOn= | grep -oE '(true|false)')" == false ] ; then
echo "Screen is off. Turning on."
adb shell input keyevent 26 # wakeup
adb shell input keyevent 82 # unlock
echo "OK, should be on now."
else
echo "Screen is already on."
fi
You've already found a solution, but I'll put this code here for reference anyway.
What you could do is to inject event to "press" the power button twice. If you don't know the status of the device (display on/off), check whether the screen is currently on or off and press the power button accordingly.
Here's a simple monkeyrunner script:
import re
from java.util import *
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device = MonkeyRunner.waitForConnection() # connect to a device
device.shell("input keyevent KEYCODE_POWER") # turn screen off (or on?)
res = device.shell("dumpsys power") # fetch power state
m = re.search(r'.*mPowerState=([0-9]+).*', res) # parse the string
if m and int(m.group(1)) == 0: # screen is off
device.shell("input keyevent KEYCODE_POWER") # turn the screen on
In addition to the answers before, here's what I do to lock / unlock my screen using adb:
adb shell input keyevent 26 will lock the screen.
So, if you execute that command again, while the screen is turned off / locked, it will be turned on / unlocked.
adb shell input keyevent 26 will also unlock the screen (if the screen is locked).
Furthermore, I have also tested all commands, such as adb shell input keyevent number, and found out that adb shell input keyevent 3 also unlock the device.
I had also found out (by testing) that key 3 is the home button. So , if you have a physical home button (not the soft home button on the screen), you can also use this to unlock your device.
For those using earlier versions of android (4.2+ at least), dumpsys power has a different output.
Instead of using mPowerState= as the answer given by #Jakub Czaplicki, I used mScreenOn=.
p = Runtime.getRuntime().exec("su", null, null);
OutputStream o = p.getOutputStream();
o.write(("dumpsys power").getBytes("ASCII"));
o.flush();
o.close();
p.waitFor();
boolean screenState;
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((res = in.readLine()) != null) dump += res;
screenState = dump.charAt( dump.indexOf("mScreenOn=") + 10 ) == 't';
screenState is true (screen on), or false (screen off).
You can use following command to trigger display ON.
adb shell input keyevent POWER
I tried and am using in my project, Hope it will work for you.
And here is the ruby code I used:
def ScreenCheck()
system("adb shell dumpsys power > c:/interact.log")
File.open("C:\\interact.log").each do |line|
if line[/mScreenOn/]
if line.strip == "mScreenOn=true"
p "Screen is On, Starting execution.."
else
p "Screen is Off, starting screen.."
system("adb shell input keyevent = POWER")
p "Starting execution.."
end
end
end
end
Here is a script to turn on/off the screen for every connected device including any pre-lollipop devices. I use this on my Jenkins server right before running any connected Android tests to make sure the devices are ready to go. Hope someone finds this useful!
#!/bin/sh
# Returns the power state of the screen 1 = on, 0 = off
getDisplayState() {
state=$(adb -s $1 shell dumpsys power | grep mScreenOn= | grep -oE '(true|false)')
# If we didn't get anything it might be a pre-lollipop device
if [ "$state" = "" ]; then
state=$(adb -s $1 shell dumpsys power | grep 'Display Power' | grep -oE '(ON|OFF)')
fi
if [ "$state" = "ON" ] || [ "$state" = "true" ]; then
return 1;
else
return 0;
fi
}
echo "Turning on screen on all connected devices..."
for device in `adb devices | grep device$ | cut -f1`
do
echo -n "Found device: $device ... "
getDisplayState $device
state=$?
# If the display is off, turn it on
if [ $state -eq 0 ]; then
echo "display was off, turning on"
adb -s $device shell input keyevent 26
else
echo "display was on"
fi
done