Batch Loop %Variable% times - android

I'm creating a batch file so I can easily control my phone using ADB
I have done most of it, however I want to be capable of controlling my volume level.
I know that adb shell input keyevent 25 decreases by 1 level, however is there a way of using variables to do it x amount of times?
So, I currently have:
cd C:\platform-tools-latest-windows\platform-tools
cls
title DECREASING Volume
cls
echo Decreasing Volume...
adb shell input keyevent 25
pause
cls
goto :USERAREA
If I were to ask the user for how many levels to decrease by, I would start with:
cd C:\platform-tools-latest-windows\platform-tools
cls
title DECREASE Volume
cls
set /p VOL=How many levels should it decrease by:
pause
echo Decreasing Volume...
adb shell input keyevent 25
pause
cls
goto :USERAREA
However, I don't know how to repeat the command adb shell input keyevent 25
%VOL% number of times.
Please help me with this,
Thank You
yg.swagness

for /L %%a in (1,1,%vol%) do adb shell input keyevent 25
see for /? from the prompt for documentation

Related

cannot close recent apps using adb shell input keyevent

I use the following code:
adb shell input keyevent KEYCODE_APP_SWITCH
adb shell input keyevent 20
adb shell input keyevent DEL
the 1st line of code works.. it does indeed open the recent app window.. however i wanted it to press the close or X button
any idea ?
you should be able to do it using the following command
adb shell input tap x y
replace x and y with the X button position on the screen.
you may simulate swipe the screen with the following command
adb shell input swipe 500 500 500 1000 100

How to bring app to Foreground in calabash-android?

I am working on a application, it contain social share options. Whenever I tap on facebook it will open facebook app in the device with share view.
After that, I want to bring my application to foreground.
I tried pressing back button,
press_back_button()
it is unsuccessful.
Is there any way to bring application to foreground in Calabash-android?
You should be able to do it by sending keypresses and screen touches with ADB
adb shell input keyevent KEYCODE_APP_SWITCH to open the recent apps menu
adb shell input tap 300 300
Will send a tap event on the screen but you'll have to play around with the coordinates to get them in the right place.
EDIT:
I just tried on my phone with the following commands and can reopen the most recent app
adb shell input keyevent KEYCODE_APP_SWITCH
adb shell input tap 300 1700
Before trying these commands on your ruby code see if they work from the terminal (not irb).
The second command uses x and y coordinates that will be different depending on the size of the screen on your phone, 300 1700 works for me but you will need to try some different numbers to make it work for you.
To open the second most recent app, which I think is what you will need, I have to run adb shell input tap 300 700. You will need to play around with this and see what works for you.
FINAL EDIT: I've found a nicer way to do this that's not dependant on screen resolution.
adb shell input keyevent KEYCODE_APP_SWITCH
Then to find your app (you may need to send these multiple times to get it to work)
adb shell input keyevent KEYCODE_DPAD_UP
and/or
adb shell input keyevent KEYCODE_DPAD_DOWN
Then to select
adb shell input keyevent KEYCODE_ENTER
you need to run the system command, e.g.
system("adb shell input keyevent KEYCODE_APP_SWITCH”)
This willl open recent activities.

Wake up Android with use adb or Eclipse (just before debug)?

How to wake up Android with use adb - I want to wake up (if asleep) Android terminal before debugging every new version of application.
Typical flow is:
1. I do some changes in Eclipse.
2. In the meantime screen goes off or not.
3. I run "debug" and want force screen to wake up.
I found a method with "power key" emulation but it does not turn it on but rather toggles the power state. I do not want to add extra code to my application. What are the other methods to do such trivial task, please help.
adb shell input keyevent KEYCODE_WAKEUP
As described here, this wakes up the device. Behaves somewhat like KEYCODE_POWER but it has no effect if the device is already awake.
To toggle sleep/wake with one command you can issue
adb shell input keyevent KEYCODE_POWER
Just use :
adb shell input keyevent 26
Here's what I came up with:
adb shell dumpsys power | grep "mScreenOn=true" | xargs -0 test -z && adb shell input keyevent 26
This will first check to see if the screen is on. If it isn't, it will emulate the power key, which will turn on the device's screen.
if you execute
adb shell input keyevent KEYCODE_POWER and got Killed, you should use root user by execute su before.
sleep:(adb shell) input keyevent KEYCODE_SLEEP
wakeup:(adb shell) input keyevent KEYCODE_WAKEUP
toggle:(adb shell) input keyevent KEYCODE_POWER
You can check device's current power state (including display) via adb with dumpsys power command and send the power key press event only if the display is off. The easier solution would be disabling the display timeout altogether "while connected to USB" in the Developer options.
you could also add the following flags to your onCreate() in your main activity:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
This way your device should wake up when it is loaded onto the device through Eclipse.
I know that this is the opposite of what the OP requested, but I wanted to point out the definition of "truly asleep" for other readers:
If adb is active, then the device is not truly asleep, since USB is on and the command prompt is running (how else is adb shell going to work?). When fully asleep, only the physical power button will wake it (unless other buttons are selected as a wake source in the BSP). My project draws about 120ma with screen off, but only 23ma in sleep mode (disconnect USB cable). "adb shell input keyevent 26" does not work in this mode. Neither does the serial console.
My test fixture does have access to the battery and external power line separately. I can toggle the power line (with battery connected) to wake it up. I also have a switched USB hub that disconnects the USB specifically for the sleep portion of the test.
I hope this can be some help to someone.
For Windows and Eclipse you can use .bat file:
#ECHO OFF
setlocal
for /f "delims= tokens=1*" %%a in ('adb shell dumpsys power ^| findstr.exe "mScreenOn="') DO (
for /f "delims== tokens=2*" %%S in ("%%a") do (
if "%%S" == "false" (goto move1) else (goto move2)
)
)
:move1
adb shell input keyevent 26
goto end
:move2
goto end
:end
exit
It will check, if screen is off it will switch the power state.
To use it with Eclipse this .bat file should be used as "External tool" (just fill the path to the .bat) and linked to the project in it's properties (Builders - Import - ).
I used the following to wake up remote devices for automated tests. If the screen isn't on, then it will press the power button followed by the menu button.
(
cd ${platform-tools};
./adb shell dumpsys power | grep -i 'SCREEN_ON' >/dev/null;
if [ $? -eq 0 ]; then
echo "Screen is on...";
else
echo "Pressing buttons to wake-up...";
# http://developer.android.com/reference/android/view/KeyEvent.html
./adb shell input keyevent 26; # Power
./adb shell input keyevent 82; # Menu
echo "Pausing for state to settle...";
sleep 1;
./adb shell dumpsys power | grep -i 'SCREEN_ON';
fi;
echo '---';
./adb shell dumpsys power | grep -i 'SCREEN'
)

How can I use adb to send a longpress key event?

I can use something like:
adb shell input keyevent 4
and this will send a single 'Back' button press to my device. How can I send a longpress?
Thanks
You can try this command:
adb shell input touchscreen swipe 170 187 170 187 2000
Your application position on screen is
170, 187; delay time is 2000 (ms);
Long press HOME key:
adb shell sendevent /dev/input/event2 1 172 1
adb shell sendevent /dev/input/event2 0 0 0
timeout 1
adb shell sendevent /dev/input/event2 1 172 0
adb shell sendevent /dev/input/event2 0 0 0
You can goto cmd and type adb shell getevent | find "event2" ; long press HOME key to see more.
Since this commit in Android 4.4 it is possibile to use:
adb shell input keyevent --longpress KEYCODE_L
This other commit further improved the behaviour.
When you want to delete something or repeat some Event or just input a lot of numbers, you can use code like the following. It will imitate a longpress on a keyboard:
adb shell input keyevent KEYCODE_FORWARD_DEL KEYCODE_FORWARD_DEL KEYCODE_FORWARD_DEL //delete 3 times
adb shell input keyevent KEYCODE_1 KEYCODE_1 KEYCODE_1 //input value '111'
You can repeat the event or input things without limits, just like a Longpress on the key. It's the same thing. You can define your own longpass and times Now
This link discusses a similar problem, but the device in question (a Nexus One device)has the menu/home/back/search buttons as part of the touchscreen, not physical keys.
This other one appears to be more inline with injecting a physical key input, but requires accessing the *.kl file for your devices driver to determine the device, type, key-code, value-press, and value-release codes for that specific device.
However, the common link between the two appears to be
adb shell sleep n
, where n is the length (in seconds) of the press duration.
Hopefully this might be of some use.
Well, this developer link show the keycode is 128, which i already test, but no expected result
You can check this link and this link. They show how to find it.
This might be too late to answer but surely will help others.
Please use below cmd to achieve long press.
adb shell input keyevent 5 sleep 5

Adb shell commands to change settings or perform tasks on a phone

How do I use adb to perform some automated tasks on my android phone? I need to find commands that I can issue from the command line (ideally, using a .bat file) that will be capable of more than simply opening an application or sending an input keyevent (button press).
For instance, I want to toggle Airplane Mode on or off from the command line. Currently, the best I can do is launch the Wireless & network settings menu and then use input keyevents to click Airplane mode:
adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS
adb shell input keyevent 19 & adb shell input keyevent 23
There are quite a few drawbacks to this method, primarily that the screen has to be on and unlocked. Also, the tasks I want to do are much broader than this simple example. Other things I'd like to do if possible:
1.Play an mp3 and set it on repeat. Current solution:
adb shell am start -n com.android.music/.MusicBrowserActivity
adb shell input keyevent 84
adb shell input keyevent 56 & adb shell input keyevent 66 & adb shell input keyevent 67 & adb shell input keyevent 19
adb shell input keyevent 23 & adb shell input keyevent 21
adb shell input keyevent 19 & adb shell input keyevent 19 & adb shell input keyevent 19 & adb shell input keyevent 22 & adb shell input keyevent 22 & adb shell input keyevent 23 & adb shell input keyevent 23
2.Play a video. (current solution: open MediaGallery, send keyevents, similar to above)
3.Change the volume (current solution: send volume-up button keyevents)
4.Change the display timeout (current solution: open sound & display settings, send keyevents)
As before, these all require the screen to be on and unlocked. The other major drawback to using keyevents is if the UI of the application is changed, the keyevents will no longer perform the correct function. If there is no easier way to do these sort of things, is there at least a way to turn the screen on (using adb) when it is off? Or to have keyevents still work when the screen is off?
I'm not very familiar with java. That said, I've seen code like the following (source: http://yenliangl.blogspot.com/2009/12/toggle-airplane-mode.html) to change a setting on the phone:
Settings.System.putInt(Settings.System.AIRPLANE_MODE_ON, 1 /* 1 or 0 */);
How do I translate something like the above into an adb shell command? Is this possible, or the wrong way to think about it?
I can provide more details if needed. Thanks!
Although question is old, it might help someone else.
For video playback, you can try this:
adb shell am start -a android.intent.action.VIEW -d "file:///mnt/sdcard/DCIM/Camera/test.3gp" -t "video/*"
^gives you a prompt of all capable players that can play this file.
adb shell am start -a android.intent.action.VIEW -d "file:///mnt/sdcard/DCIM/Camera/test.3gp" -t "video/*" -n "com.alensw.PicFolder/.PlayerActivity"
^plays in player specified by switch -n.
I'm working on the same set of issues. (I mostly solved the context issue with straight button presses by using the keyevent HOME and then MENU, but -- somehow -- even that's unreliable.) I'm currently investigating SL4A (Scripting Layer for Android), which has promise. It allows Perl, Python, Lua, and other scripting languages to interact with the Android API from your PC after starting an SL4A server on the device -- which can also be done from the PC. I'm finding "Pro Android Python with SL4A" to be an excellent resource; I would have saved myself days of trial-and-error and hunting on the Web if I had started with that book
The Java example you show is for a program that is running on the phone itself. You might be able to program some kind of an interpreter on the phone that handles adb commands. That way you are not dependent anymore on keyevents. This is not a minor undertaking, however.
MonkeyRunner also looks like it has promise, though I haven't dug deep enough to find its limits yet.
Android ScreenCast lets you view and control your device's screen from your PC, which also has potential for automation. It does have present some logistical issues for that application, though.
By the way, you can get past the locked homescreen with a MENU keyevent and a set of sendevents (in place of keyevents).

Categories

Resources