Vaibhav Joshi
Vaibhav Joshi

Reputation: 221

ADB: How to tap/close from recent apps to completely through ADB one liner command

I have tried multiple commands to close the app from App switcher but didn't through it. I even don't know if there is ány adb command which is persist to close the app from the app switcher. I more Googled for the same and even went through Android but no luck. Does anyone know about this, how to tackle? Kindly suggest our valuable inputs. Thanks in advance

Upvotes: 11

Views: 23638

Answers (8)

user1874594
user1874594

Reputation: 2483

On Android 10 .29 dumpsys command gives details of recent processes and at the end of the long o/p if gives a summary. Focus on the summary as your grep ground and from it get stack id and then am remove it . Works Here's what the filtered output looks like.

    dumpsys activity recents | grep -i stackid                                                                                    
+ dumpsys activity recents
+ grep -i stackid
  * Recent #0: TaskRecord{a95fb69 #579 A=com.microsoft.emmx U=0 StackId=199 sz=1}
    stackId=199
  * Recent #1: TaskRecord{130e59b #474 I=com.sec.android.app.launcher/com.android.quickstep.RecentsActivity U=0 StackId=16 sz=1}
    stackId=16
  * Recent #2: TaskRecord{9d85584 #568 A=in.amazon.mShop.android.shopping U=0 StackId=198 sz=1}
    stackId=198
  * Recent #3: TaskRecord{37f1865 #462 I=com.teslacoilsw.launcher/.NovaLauncher U=0 StackId=0 sz=1}
    stackId=0
  * Recent #4: TaskRecord{7c626b8 #572 A=com.android.settings U=0 StackId=191 sz=3}
    stackId=191
  * Recent #5: TaskRecord{9cc6f48 #570 A=app.revanced.android.youtube U=0 StackId=183 sz=1}
    stackId=183
  * Recent #6: TaskRecord{c369f36 #569 A=com.flipkart.android U=0 StackId=167 sz=1}
    stackId=167
  * Recent #7: TaskRecord{9b5d50f #567 A=com.samsung.android.messaging U=0 StackId=-1 sz=0}
    stackId=-1
  * Recent #8: TaskRecord{347f9a9 #521 I=com.google.android.apps.nbu.paisa.user/com.google.nbu.paisa.flutter.gpay.app.LauncherActivity U=0 StackId=97 sz=1}
    stackId=97
 id=579 stackId=199 userId=0 hasTask=true lastActiveTime=45318139
id=568 stackId=198 userId=0 hasTask=true lastActiveTime=45310442
id=572 stackId=191 userId=0 hasTask=true lastActiveTime=43355621
id=570 stackId=183 userId=0 hasTask=true lastActiveTime=42951254
id=569 stackId=167 userId=0 hasTask=true lastActiveTime=31632459
id=567 stackId=-1 userId=0 hasTask=false lastActiveTime=31541638
id=521 stackId=97 userId=0 hasTask=true lastActiveTime=28381669
id=562 stackId=158 userId=0 hasTask=true lastActiveTime=28224296
id=491 stackId=64 userId=0 hasTask=true lastActiveTime=23406974
id=553 stackId=149 userId=0 hasTask=true lastActiveTime=21094418
id=546 stackId=134 userId=0 hasTask=true lastActiveTime=19998067
id=542 stackId=129 userId=0 hasTask=true lastActiveTime=18724286
id=488 stackId=67 userId=0 hasTask=true lastActiveTime=3719919
id=490 stackId=62 userId=0 hasTask=true lastActiveTime=3194314

I have written below function that will implement above logic. cr as in clear recents

cr1() {                                  
dumpsys activity recents | awk '/id=[0-9]+ stackId=[0-9]+/ {print $2}' | sed 's/stackId=//g' | while read stack_id; do
  echo "stack id is $stack_id";
  am stack remove $stack_id;     
done                           
}

running it here's the o/p from adb

JohnDoe.///#cr1                                                                                                                           
stack id is 209
stack id is 46
stack id is 208
stack id is 207

Upvotes: 0

I wrote shell script that will close all recent apps.

#!/bin/bash

# When this is executed:
# ```
# adb shell dumpsys activity recents | grep 'Recent #0'
# ```
#
# The output will be like this:
# ```
#   * Recent #0: Task{46c88ca #115 type=standard A=10162:com.lifemd.care.stage}
#   * Recent #1: Task{cb4f5ae #111 type=home I=com.google.android.apps.nexuslauncher/.NexusLauncherActivity}
# ```
#
# The output is filtered to get only `standard` type tasks and then their IDs are extracted.
# The IDs are then used to close the recent apps.
close_all_recent_apps() {
  # List all standard tasks in the recent apps
  local tasks
  tasks=$(adb shell dumpsys activity recents | grep "Recent #" | grep "type=standard")

  # Split the output into an array
  IFS=$'\n' read -rd '' -a tasks <<<"$tasks"

  # Close each recent app
  for task in "${tasks[@]}"; do
    local task_id
    task_id=$(echo "$task" | sed -n 's/.*#\([0-9]*\).*/\1/p')
    echo "Closing the recent app with ID: $task_id"
    adb shell am stack remove "$task_id"
  done
}

close_all_recent_apps

Upvotes: 0

Babbo Natale
Babbo Natale

Reputation: 179

  1. Get stack id
    adb shell dumpsys activity recents

##########result example RecentTask 0

RecentTaskInfo #0:
    id=170 userId=0 hasTask=true lastActiveTime=102664977
    baseIntent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x
10200000 cmp=com.android.vending/.AssetBrowserActivity }
    baseActivity={com.android.vending/com.android.vending.AssetBrowserActivity}
    topActivity={com.android.vending/com.google.android.finsky.activities.MainActivity}
    origActivity={com.android.vending/com.android.vending.AssetBrowserActivity}
    realActivity={com.android.vending/com.google.android.finsky.activities.MainActivity}
    isExcluded=false activityType=standard windowingMode=fullscreen supportsSplitScreenMultiWindow=t
rue supportsMultiWindow=true
    taskDescription { colorBackground=#fffafafa colorPrimary=#ffffffff iconRes=/0 iconBitmap=false r
esizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION minWidth=-1 minHeight=-1 colorBackgroundFloating=#f
fffffff }
    lastSnapshotData { taskSize=Point(1080, 2400) contentInsets=Rect(0, 91 - 0, 126) bufferSize=Poin
t(1080, 2400) }
  1. Remove stack id 170 (play store)
    adb shell am stack remove 170

Upvotes: 3

walkman
walkman

Reputation: 1861

#get apps
dumpsys window windows | grep -P 'topApp.* u0 ([^/]+)' | grep -P '(?<= u0 )[^/]+' | xargs -l am force-stop

Upvotes: 0

HackRx
HackRx

Reputation: 318

Thanks to @rilwan for the answer, below command worked for me: First enter into adb shell then execute:

$ adb shell
$ input keyevent KEYCODE_APP_SWITCH && input swipe 522 1647 522 90

or execute at once

$ adb shell input keyevent KEYCODE_APP_SWITCH && input swipe 522 1647 522 90

Upvotes: 3

Mustafa SEN
Mustafa SEN

Reputation: 29

android 5.0 and more

open recent apps

adb shell input keyevent KEYCODE_APP_SWITCH

select app

adb shell input keyevent KEYCODE_DPAD_DOWN

clear from recent apps

adb shell input keyevent DEL

Upvotes: 2

Vaibhav Joshi
Vaibhav Joshi

Reputation: 220

Finally, I got the answer when I tried combining multiple commands:

  1. Open the app switcher

    adb shell input keyevent KEYCODE_APP_SWITCH
    
  2. Select or navigate to the next app in the app switcher

    adb shell input keyevent 20
    ...
    

    (run the above command again for each app down the list)

  3. Remove the app from open app list

    adb shell input keyevent DEL
    

And you are done :-) the app gone out of your open apps list.

Upvotes: 18

Rilwan
Rilwan

Reputation: 2301

Here are some tips-

  1. Open recent apps-

adb shell input keyevent KEYCODE_APP_SWITCHER

  1. Tap the cross X or close all . Depends on your system UI you need to navigate to it and press enter. For me below worked, you can find what works for your device using up,down,tab KEYEVENTs.

adb shell input keyevent KEYCODE_DPAD_DOWN

adb shell input keyevent KEYCODE_ENTER

Upvotes: 0

Related Questions