Can an Android app detect ADB commands? - android

Is there anyway an app could detect if it was launched (or killed) via an ADB command line code from an attached computer vs physically tapping the actual app icon on the screen?
And/or could it tell if a tap input in the app was done by a physical tap vs an ADB input?
Launch command: adb shell monkey -p -c android.intent.category.LAUNCHER 1
Kill command: adb shell am force-stop
Tap command: adb shell input tap

adb command is running in a brandnew process of system/bin/sh. It is difficult to achieve,even if the device is rooted.
You maybe need handle touch events via getevent command. adb shell input will skip the input device to simulate user action, but if someone uses the adb shell sendevent command, you will not be able to detect it.

Related

Wait for Android emulator to be running before next shell command?

I started an Android emulator using the following shell command:
emulator -avd TEST_AVD
The emulator starts just fine, but the shell script never finishes executing. It just hangs there even after the emulator has completed startup. I have tried with a number of other arguments that I could find, but nothing could quite do what I want it to. How do I know, or stop the shell command, when the emulator is ready to go?
I am setting up our Jenkins CI to use a Jenkinsfile to start the emulator, and then run a series of gradle commands. In short, I'd like to do this:
sh "emulator -avd TEST_AVD"
sh "./gradlew clean test spoon"
However, I don't want to run the gradle tasks until the emulator has finished startup, and I can't figure out how to do that in the terminal.
If you want to do something after you start the emulator you should start it in the background
emulator -avd TEST_AVD &
adb wait-for-device
# other stuff here
adb can wait for a device over a transport to be in a particular state
adb wait-for[-<transport>]-<state>
- wait for device to be in the given state:
device, recovery, sideload, or bootloader
Transport is: usb, local or any [default=any]
To wait until device (or emulator) boots, you can do something like this (as was already answered by Пионерка):
adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;'
Basically:
Wait for device/emulator to be in adb device state
Open shell & sleep in 1 second intervals until the sys.boot_completed property becomes true
If anybody would be interested what Android Studio does, when running emulator, the answer is this class:
If device is online, then it is ready. No need to go to further steps.
Checks system property adb shell getprop dev.bootcomplete until it is equal to 1
For API 23+ devices runs command to unlock screen: adb shell wm dismiss-keyguard
Waits 1 second.

Detecting if emulator has started completely

I want to check if all the emulators has booted successfully. I have tried with this command adb shell getprop sys.boot_completed this works if i am running one emulator. But if i have more than one emulator this command returns error as following: error: more than one device/emulator.How to solve this?
You should specify the device serial number on the adb command line to let it know which one you want to interact with.
Something like
adb -s emulator-5554 shell ...
Also, if you are doing it from the command line, multiple times and you are starting to be annoyed by that, you can try https://gist.github.com/dtmilano/4537110 which allows you to select the device
$ adb shell
1) 02783201431feeee device 3) emulator-5554
2) 3832380FA5F30000 device 4) emulator-5556
Select the device to use, <Q> to quit: 1
$

python and (android) adb shell

i would like to simulate touch on my anroid phone through a python code on my computer using the "adb shell tap x y" function (or any other way you may know). I have tried using
from subprocess import call
call(["adb", "kill-server"])
call(["adb", "shell"])
call(["input", "tap" , "1400" , "800"]) //example of x and y
but it just reaches the "shell" call and gets stuck.
(I know the tap function works because it works on the ordinary cmd window)
This should do it:
from subprocess import call
call(["adb", "shell", "input", "tap" , "1400" , "800"])
In your original script:
You start a remote shell on your Android device (adb shell)
After you quit the remote shell typing exit, you issue a command on your host computer shell (input tap 1400 800).
Instead you should use adb to redirect a command to the Android device's remote shell. To do that, just append the command after adb shell, for example adb shell input tap 1400 800. Take a look here.
I also removed the adb kill-process line because there is no kill-process adb command.

Running specific commands in monkey not working

I read this page
http://hariniachala.blogspot.com/2011/09/android-application-ui-testing-with.html
and when I executed this code
./adb -d shell monkey -p package_name --port 1080 &
./adb -d forward tcp:1080 tcp:1080
telnet localhost 1080
I faced the following problem
C:\Users\subhi\Desktop>adb -d shell monkey -p package_name --port 1080
error: device not found
what is the simplest method to do that ?
probably the reason why ADB is not finding a device is that you are forcing the search for a physical one (-d option). If you want to interact with an emulated device you should use -e or even no option (ADB should resolve it automatically).
Summing up, if I understood your problem, my suggestion is to launch an AVD, wait until boot and use:
adb shell monkey -p <app.package.name> --port <X> &
adb forward tcp:<X> tcp:<X>
telnet localhost <X>
where
<X>
is the port number you want to use.

Start a process in background from adb shell without attaching the process to the terminal in Android

I have a simple problem.
I want to start/run a program file on an android phone using adb shell.
My Program is in /system/bin folder and has root privileges.
I want to run this program from my command prompt as adb shell runme 3000000 > logs.txt but it should not block the terminal, It should run in background.
I cannot use screen/disown/nohup for my problem as android doesn't have all this.
I tried
adb shell "runme >logs.txt &" but of no use.
When i issue command as
adb shell
# runme 3000000 > logs.txt &
It runs fine, when i exit the terminal/disconnect the device and then connect again to system.
Do adb shell ps | grep runme shows the process is still runnning in background.
Thanks
Busybox has nohup applet which works just fine in Android

Categories

Resources