从控制台停止 Android 应用程序 [英] Stopping an Android app from console

查看:42
本文介绍了从控制台停止 Android 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从控制台停止 Android 应用程序?类似的东西:

Is it possible to stop an Android app from the console? Something like:

adb stop com.my.app.package

这会大大加快我们的测试过程.现在我们每次都卸载/安装应用程序,以确保手动测试用例以干净的状态开始.

It would speed up our testing process so much. Right now we uninstall/install the app each time to make sure the manual test cases start with a clean state.

推荐答案

在我写完这篇文章并被接受作为答案很久之后,我强制停止code> 命令由 Android 团队实施,如这个答案.

Long after I wrote this post and it was accepted as the answer, the am force-stop command was implemented by the Android team, as mentioned in this answer.

或者:与其只是停止应用程序,因为您提到每次测试运行都需要干净的石板",您可以使用 adb shell pm clear com.my.app.package,这将停止应用进程清除该应用的所有存储数据.

Alternatively: Rather than just stopping the app, since you mention wanting a "clean slate" for each test run, you can use adb shell pm clear com.my.app.package, which will stop the app process and clear out all the stored data for that app.

如果您使用的是 Linux:
adb shell ps |grep com.myapp |awk '{print $2}' |xargs adb shell kill

If you're on Linux:
adb shell ps | grep com.myapp | awk '{print $2}' | xargs adb shell kill

这仅适用于在运行 shell 后立即拥有 root 权限的设备/模拟器.这可能可以稍微改进以预先调用 su.

That will only work for devices/emulators where you have root immediately upon running a shell. That can probably be refined slightly to call su beforehand.

否则,你可以做(​​手动,或者我想脚本):
pc $ adb -d shell
android $ su
android #ps
android # kill <来自ps输出的进程id>

Otherwise, you can do (manually, or I suppose scripted):
pc $ adb -d shell
android $ su
android # ps
android # kill <process id from ps output>

这篇关于从控制台停止 Android 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆