从Android获取进程PID以杀死

时间:2018-06-22 10:59:16

标签: c# adb

我想使用C#从Android SP中通过PID终止进程 通常我是在命令行写的,如屏幕截图所示

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

73254是PID,但每次都会更改。 那么如何使用C# enter image description here

1 个答案:

答案 0 :(得分:-1)

# 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