How to bring app to Foreground in calabash-android? - 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.

Related

adb send keystroke doesn't work

I'm trying to emulate input from my android application to make some test.
Well, my Android phone (AREO 8.1.0) is connected to USB and my debugger is enabled.
I use some
adb shell input tap 90 310
adb shell input text "banana"
and my keyboard open as well with "banana" text in the text field (keyboard and apps textfield)
The problem is the next step:
adb shell input keyevent 66 # ENTER input
It won't send this command.
I'm thinking its about my custom keyboard.
I use
to find whats the keyevent I need.
I also use the
keyevent KEYCODE_ENTER
keyevent KEYCODE_TAB
...
the keyevent BACK, SPACE, CLEAR works as well but not the ENTER
*by the way, I tried with and without --longpress
Anyone know what is the problem?

Tell Smartphone to send SMS with PC

Is there a way to do the following instructions using Android on phone and Windows on computer?
Computer: Reads numbers and meeting-dates out of a database.
Computer: Connects to the smartphone (via USB?) and uses its "send SMS"-function
Smartphone: Sends SMS with given Text.
Or do i have to use Third Party Email to SMS tools? I'd like to avoid that.
You can use USB debugging and some faked user input.
First, enable USB debugging. Depending on your Android version and device model, this may be either an option somewhere in the normal settings app or in the hidden developer mode which you can access by tapping the build number under "Status" 7 times. Also, you may be asked to allow access from your computer the first time you try to do anything with this feature.
Then, download ADB and any driver needed for your device, if any. ADB is a command-line tool to send debugging commands to your device.
You can use adb devices in the console to see if things work - you should see your device listed.
The idea is now to start the SMS app with recipient and text already filled in (which is a supported action) and then fake the user clicking "Send". This is where the tricky part lies. Depending on your device, there can be different key input required than for mine, for example. Usually you will need to send one or more D-Pad presses plus "Enter" (yes this works even if the device doesn't have a D-Pad).
The commands you'll need:
adb shell am start -a android.intent.action.SENDTO -d sms:<full phone number here> --es sms_body "<SMS text here>" --ez exit_on_sent true
This will open the SMS app with the values already filled in. Replace <full phone number here> with the phone number, and <SMS text here> with your text. If you need to use quotes inside the text, you have to escape them like \".
adb shell input keyevent <id>
This will send a fake keypress. The possible IDs are listed here. Particularly interesting are:
19: D-Pad up
20: D-Pad down
21: D-Pad left
22: D-Pad right
23: D-Pad center
66: Enter
You will need to play with these. You probably need to send 22 or 20 to simulate a "right" or "down" press to focus the "Send" button followed by 23 or 66 to simulate a press on the D-Pad center or enter key to "click" the button.
So, for example, it could look like this:
adb shell am start -a android.intent.action.SENDTO -d sms:+436501234567 --es sms_body "This is a test" --ez exit_on_sent true
adb shell input keyevent 20
adb shell input keyevent 66
This would open the SMS app with the number +436501234567 and the text This is a test already filled in and then simulate the "down" key and the "enter" key.
Please give the whole thing some time. Add some delays (at least 1s) between each command.

Android ADB: Simulate Recent App key press

Is there a way to get the use ADB to simulate a Recent App key press? I do not see it in the list of assigned keys and I am not able to select it using adb input shell tap despite getting the coordinates from the debugging options.
I am trying to automate a task on my own personal phone (Nexus 5 with soft keys) so any hacky way is fine, assuming there is not a clean way to do this.
The solution is to use the KEYCODE_APP_SWITCH KeyEvent:
adb shell input keyevent KEYCODE_APP_SWITCH
See #SimonMarquis answer below
adb shell input keyevent KEYCODE_APP_SWITCH
(OBSOLETE)
I don't think there is a keycode for it.
However, I am able to open the recent apps menu with adb shell input tap (testing on a Nexus 5 with Lollipop). For example:
adb shell input tap 800 1890
adb shell input keyevent KEYCODE_APP_SWITCH
Still works, you don't even have to put its numeric value (187) into the command.

Turn on screen on device

How can I turn the sceen on ?
I tried something like this
adb -d shell am broadcast -a android.intent.action.SCREEN_ON
It really should work, I send broadcast intent it is received by the system, but the screen doesn't turn on
I do not understand what is the problem, is it possible to turn the screen of the device by code ? I mean with software ? Cause it seems like the turning on of the screen is done just by the hardware button press . . . at least I got that felling , am I wrong ?
adb shell input keyevent KEYCODE_POWER
Works to turn on screen (when display is off)
Works to turn off screen (when display is on/awake)
For Android 5.0 and above:
adb shell input keyevent KEYCODE_WAKEUP
or
adb shell input keyevent 224
KEYCODE_WAKEUP
Key code constant: Wakeup key. Wakes up the device. Behaves somewhat like KEYCODE_POWER but it has no effect if the device is already awake.
https://developer.android.com/reference/android/view/KeyEvent#KEYCODE_WAKEUP
Note: KEYCODE_POWER has been added in API level 1, while KEYCODE_WAKEUP has been added in API level 20!
u can turn it on/off if u do like:
adb shell
#shell: input keyevent 26
#shell: (enter or via hidden command empty line)
#shell: exit
this worked for me on some android versions ;)(NOTE: this will turn the screen on and off, depends on the actual screen state)
To detect the current state of the screen u can use the following ways:Android < 5.x.xadb shell dumpsys input_methodIn the output search for mScreenOn=true/false
Android >= 5.x.xadb shell dumpsys displayIn the output search for mScreenState=ON/OFF
In my scripts i use this \s{0,}mScreen(State|On)=(?<STATE>(true|false|on|off))\s{0,} (Compiled|IgnoreCase|ExplicitCapture) regular expression for both outputs to detect the current state.
EDIT (16.03.2018):
There is also another way to detect the screen state, it works since Android 3.0. The dumpsys window policy command will give us all we need. - In the output search for mScreenOn(Fully)?=(?<STATE>(true|false)).
There are also other useful informations like:
mUnrestrictedScreen (value is like: (0,0) 768x1280)
mRestrictedScreen (value is like: (0,0) 768x1184)
Regards,
k1ll3r8e
I could be wrong about this, but...
You shouldn't think of broadcasts as something to send to get things done, but instead think of them as things that are sent when things are done.
I think the system sends 'android.intent.action.SCREEN_ON' when screen is goes on, but sending 'android.intent.action.SCREEN_ON' does not necessarily make the screen go on.
I hope this makes sense.
For the answer, you can find it in...
Calling hidden API in android to turn screen off
turn the screen on/off in Android with a shake
The command to toggle the screen on/off is:
adb shell input keyevent 26
This condensed command is preferred because it allows you to use it in scripts.
Cheers!
this works in android 12
#!/bin/bash
screenState=$(adb shell dumpsys window policy | grep screenState=SCREEN_STATE_ | cut -c 32-)
if [ "$screenState" == "OFF" ]; then
adb shell input keyevent KEYCODE_POWER
fi

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