Search code examples
c#adb

Get process PID from android to kill


I want to kill process by PID from android SP using C# usually I write in commande line as shown the screenshoot

adb shell
run-as "com.android.commands.monkey"
ps | grep "com.android.commands.monkey"
kill -9 73254

73254 is the PID but it will change every time. So how do it with C# enter image description here


Solution

  • # Kill myphoneexplorer process on SP
    adb shell "am force-stop com.fjsoft.myphoneexplorer.client"
    sleep 2
    # Kill com.android.commands.monkey process on SP
    adb shell "run-as com.android.commands.monkey"
    adb shell "ps | grep com.android.commands.monkey"
    for item in $(adb shell "ps | grep com.android.commands.monkey"); do
        if test "$item" != ""
        then
            arr+=($item)
            echo ${arr[1]}
            adb shell "kill -9 ${arr[1]}"
        fi
    
    done
    sleep 2
    sleep 10