Unpin application with adb shell commands in android lollipop - android

Summary
Is there a way to unpin apps in lollipop using shell commands
with adb iv tried:
adb shell input keyevent --longpress 4 187
but this never worked
Why i want to do this
1) i'm making a root application that replaces the on screen navigation buttons and need a way to unpin other apps
2) when using a virtual device and i pin a app there is no way for me to press back and recents at the same time with my mouse

Try this command to unpin the foreground app:
am task lock stop `dumpsys activity | grep -A2 "(dumpsys activity recents)"| grep '#'| cut -d ' ' -f 7| cut -c 2-`

adb shell am task lock stop `dumpsys activity | grep -A2 '(dumpsys
activity recents)'| grep '#'| cut -d ' ' " \
"-f 7| cut -c 2-
This works with Android 9+ devices

This works with android 4.4.2
adb shell su -c 'sqlite3 /data/data/com.android.launcher/databases/launcher.db "DELETE from favorites"'

Related

Unable To get Activity name from adb command Android 10

I'm trying to get activity name for current focused app on the device by running this command:
adb shell "dumpsys window windows | grep -E 'mCurrentFocusApp|mFocusedApp'"
It works on older phone with Android 6.0 but on Pixel XL running Android 10 its returns nothing.
If I run only dumpsys window windows it returns bunch of unfiltered info which is not very efficient for me.
adb shell dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8
John answer is great but u can use:
adb shell dumpsys window windows | grep mActivityRecord
in that case u get list of all current run app in memory
if u kill all app and run the one u want u get 2 records
# launcher
mActivityRecord=ActivityRecord{99197dc u0 com.sec.android.app.launcher/.activities.LauncherActivity t3161}
# active app
mActivityRecord=ActivityRecord{6dec4d5 u0 com.google.android.googlequicksearchbox/com.google.android.apps.gsa.monet.MonetActivity t3711}
adb shell dumpsys activity activities | grep "mFocused"
It worked on android 10 & 11.

why i can't get adb shell top works?

I'm testing an android app by my PC. I try to catch the CPU usage of it.
if I use
adb shell top -m 10 |tee aa.txt
it works fine.
but if i modify it like this
adb shell top -m 10 |grep myAPPname|tee aa.txt
it can not. Why and how to make it work again?
try the command below:
while true; do
adb shell top -m 10 -n 1 | grep kso >> aa
done
-n means only show the current status instead of updating frequently.
However, I suggest you use Android Profiler which is a built-in function in Android Studio to achieve your monitoring work.

Kill processes from shell in Android

I try to find some way to kill unnecessary services/processes in Android from shell.
The problem is that after killing the process it starts again after few seconds!
for example I tried to kill batterywarning, but it keep starting again:
root#w812a_kk:/ # ps | grep batteryw
shell 17986 1 1044 364 c00601dc b6e9f094 S /system/bin/batterywarning
1|root#w812a_kk:/ # ps | grep batteryw
shell 17781 1 1044 364 c00601dc b6ee6094 S /system/bin/batterywarning
root#w812a_kk:/ # busybox killall batterywarning
root#w812a_kk:/ # ps | grep batteryw
1|root#w812a_kk:/ # ps | grep batteryw
shell 17986 1 1044 364 c00601dc b6e9f094 S /system/bin/batterywarning
I did find several methods to kill service/process in the following link, yet the process is starting again.
Android ADB stop application command like "force-stop" for non rooted device.
Is it something that can only be done in init.rc ?
Thanks,
This service is probably started as STICKY - it will be automatically restarted by OS after some predefined timeout.
You can check it by pulling the logcat from the device and grepping it for the name of the service you attempted to kill:
$ adb logcat -d > logcat.txt
$ grep -C 5 -i batterywarning logcat.txt
If you see something along the lines scheduling restart of crashed service in X seconds then you know for sure that the service is being restarted by OS.
If it is indeed the case, I doubt that you can kill it completely on a non-rooted device (neither I know how to achieve this on a rooted one).
kill a process when you have the process name (basically you don't have to search 'ps' for the process ID and then kill it with PID):
adb shell kill $(pidof com.android.phone)
Use the following command from terminal:
adb shell am crash [applicationId]

Running adb commands on all connected devices

Is there a way of running adb commands on all connected devices? To uninstall an app from all connected devices with "adb uninstall com.example.android".
The commands I am interested in is mainly install and uninstall.
I was thinking about writing a bash script for this, but I feel like someone should have done it already :)
Create a bash file and name it e.g. adb+:
#!/bin/bash
adb devices | while read -r line
do
if [ ! "$line" = "" ] && [ "$(echo "$line" | awk '{print $2}')" = "device" ]
then
device=$(echo "$line" | awk '{print $1}')
echo "$device" "$#" ...
adb -s "$device" "$#"
fi
done
Usage: ./adb+ <command>
Building on #Oli's answer, this will also let the command(s) run in parallel, using xargs. Just add this to your .bashrc file:
function adball()
{
adb devices | egrep '\t(device|emulator)' | cut -f 1 | xargs -t -J% -n1 -P5 \
adb -s % "$#"
}
and apply it by opening a new shell terminal, . ~/.bashrc, or source ~/.bashrc.
If you only want to run on devices (or only on emulators), you can change the (device|emulator) grep by removing the one you don't want. This command as written above will run on all attached devices and emulators.
the -J% argument specifies that you want xargs to replace the first occurrence of % in the utility with the value from the left side of the pipe (stdin).
NOTE: this is for BSD (Darwin / Mac OS X) xargs. For GNU/Linux xargs, the option is -I%.
-t will cause xargs to print the command it is about to run immediately before running it.
-n1 means xargs should only use at most 1 argument in each invocation of the command (as opposed to some utilities which can take multiple arguments, like rm for example).
-P5 allows up to 5 parallel processes to run simultaneously. If you want instead to run the commands sequentially, simply remove the entire -P5 argument. This also allows you to have two variations of the command (adball and adbseq, for example) -- one that runs in parallel, the other sequentially.
To prove that it is parallel, you can run a shell command that includes a sleep in it, for example:
$ adball shell "getprop ro.serialno ; date ; sleep 1 ; date ; getprop ro.serialno"
You can use this to run any adb command you want (yes, even adball logcat will work! but it might look a little strange because both logs will be streaming to your console in parallel, so you won't be able to distinguish which device a given log line is coming from).
The benefit of this approach over #dtmilano's & approach is that xargs will continue to block the shell as long as at least one of the parallel processes is still running: that means you can break out of both commands by simply using ^C, just like you're used to doing. With dtmilano's approach, if you were to run adb+ logcat, then both logcat processes would be backgrounded, and so you would have to manually kill the logcat process yourself using ps and kill or pkill. Using xargs makes it look and feel just like a regular blocking command line, and if you only have one device, then it will work exactly like adb.
This is an improved version of the script from 強大な. The original version was not matching some devices.
DEVICES=`adb devices | grep -v devices | grep device | cut -f 1`
for device in $DEVICES; do
echo "$device $# ..."
adb -s $device $#
done
To add in the ~/.bashrc or ~/.zshrc:
alias adb-all="adb devices | awk 'NR>1{print \$1}' | parallel -rkj0 --tagstring 'on {}: ' adb -s {}"
Examples:
$ adb-all shell date
$ adb-all shell getprop net.hostname
$ adb-all sideload /path/to/rom.zip
$ adb-all install /path/filename.apk
$ adb-all push /usr/local/bin/frida-server-arm64 /data/local/tmp/frida-server
Explanation: awk extracts the device id/host (first column: print $1) of every lines except the first one (NR>1) to remove the "List of devices attached" header line), then gnu parallel runs adb -s <HOSTNAME> <whatever-is-passed-to-the-alias> on whatever non-empty line (-r) in the order specified (-k, to avoid random order / fastest response order) and prepend each line with on <DEVICE>:\t for clarity, all in parallel (-j0, possible to set another number to define how many adb should be ran in parallel instead of unlimited).
:)
This is the highest result on Google, so for all Windows users coming here let me add this solution by User zingh (slightly modified to accept arbitrary commands, rather than "only" install
Batch file (adball.bat):
FOR /F "skip=1" %%x IN ('adb devices') DO start adb -s %%x %*
Call as:
adball uninstall com.mypackage
(%* takes all input parameters, my line above makes it so that all commands are passed to adb as they are, so that you can type multiple words, flags etc.)
Note: you can even use this directly from the Android Studio "run all" popup, if you install the Powershell-plugin. You can add adball to your path, then double-tap ctrl and run
powershell adball uninstall com.mypackage
adb wrapper supports selecting multiple targets for adb commands and parallel execution.
From its README:
# Installation
./install.sh ~/apps/android-sdk-linux
# Execute adb commands on all connected devices.
adb set-target all
# Execute adb commands on given devices.
adb set-target emulator-5554 C59KGT14263422
# Use GNU parallel for parallel install.
adb set-parallel true
(Disclaimer: I have written half of it)

How to kill native applications from 'adb shell'?

I am able to start native applications using am start -a action -n packagename/activity. How can I kill/stop a native application from adb shell?
adb shell am force-stop packagename
Chirag deleted it, so here it is again:
adb shell ps | grep com.myapp | awk '{print $2}' | xargs adb shell kill
This is to be run outside of the emulator. It is one long Unix command, not four commands with a visual separation. | is syntax, interpreted by your (Ubuntu's) shell, which then pipes the output from adb, grep, etc., into the next. Only ps is executed in the emulator.
Another way to kill your app is to send your app to backround (using home button) and call:
adb shell am kill com.your.package
It works not on all of my devices, however, on devices where it works it behaves the same way as if the app was killed in background due to lack of resources and this state is often handy to test different aspects of your process recreation.
For example, if you have Broadcast Receivers registered in Manifest, they will still start without restarting your app manually, comparing to force stop that you can achieve using:
adb shell am force-stop com.your.package
Please try the below command in adb shell.
adb shell kill <PID>

Categories

Resources