从控制台停止 Android 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3117095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:46:59  来源:igfitidea点击:

Stopping an Android app from console

androidtestingadb

提问by hpique

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

是否可以从控制台停止 Android 应用程序?就像是:

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.

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

采纳答案by Christopher Orr

Edit:Long after I wrote this post and it was accepted as the answer, the am force-stopcommand was implementedby the Android team, as mentioned in this answer.

编辑:在我写完这篇文章并被接受为答案很久之后,该am force-stop命令是由 Android 团队实现的,如本答案中所述

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 andclear out all the stored data for that app.

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



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

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

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

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

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

否则,你可以做(​​手动,或者我想脚本):
pc $ adb -d shell
android $ su
android # ps
android # kill <process id from ps output>

回答by Enrico Ros

The clean way of stopping the app is:

停止应用程序的干净方法是:

adb shell am force-stop com.my.app.package

This way you don't have to figure out the process ID.

这样您就不必找出进程 ID。

回答by dell116

First, put the app into the background (press the device's home button)

首先,将应用程序置于后台(按设备的主页按钮)

Then....in a terminal....

然后......在终端......

adb shell am kill com.your.package

回答by Someone Somewhere

you can use the following from the device console: pm disable com.my.app.packagewhich will kill it. Then use pm enable com.my.app.packageso that you can launch it again.

您可以从设备控制台使用以下内容:pm disable com.my.app.package这将杀死它。然后使用,pm enable com.my.app.package以便您可以再次启动它。

回答by ThomasW

If you have access to the application package, then you can install with the -r option and it will kill the process if it is currently running as a side effect. Like this:

如果您有权访问应用程序包,那么您可以使用 -r 选项进行安装,如果它作为副作用当前正在运行,它将终止该进程。像这样:

adb -d install -r MyApp.apk ; adb -d shell am start -a android.intent.action.MAIN -n com.MyCompany.MyApp/.MyActivity

adb -d install -r MyApp.apk ; adb -d shell am start -a android.intent.action.MAIN -n com.MyCompany.MyApp/.MyActivity

The -r option preserves the data currently associated with the app. However, if you want a clean slate like you mention you might not want to use that option.

-r 选项保留当前与应用程序关联的数据。但是,如果您想要像您提到的那样干净利落,您可能不想使用该选项。

回答by Patrick Valsecchi

If you target a non-rooted device and/or have services in you APK that you don't want to stop as well, the other solutions won't work.

如果您的目标是非 root 设备和/或 APK 中有您不想停止的服务,则其他解决方案将不起作用。

To solve this problem, I've resorted to a broadcast message receiver I've added to my activity in order to stop it.

为了解决这个问题,我求助于我添加到我的活动中的广播消息接收器来阻止它。

public class TestActivity extends Activity {
    private static final String STOP_COMMAND = "com.example.TestActivity.STOP";

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            TestActivity.this.finish();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //other stuff...

        registerReceiver(broadcastReceiver, new IntentFilter(STOP_COMMAND));
    }
}

That way, you can issue this adb command to stop your activity:

这样,您可以发出此 adb 命令来停止您的活动:

adb shell am broadcast -a com.example.TestActivity.STOP

回答by steve

If all you are looking for is killing a package

如果您正在寻找的只是杀死一个包

pkill package_name 

should work

应该管用

回答by sinan bilkay

adb shell killall -9 com.your.package.name

adb shell killall -9 com.your.package.name

according to MAC "mandatory access control" you probably have the permission to kill process which is not started by root

根据MAC“强制访问控制”,您可能有权杀死不是由root启动的进程

have fun!

玩得开心!

回答by matto1990

In eclipse go to the DDMS perspective and in the devices tab click the process you want to kill under the device you want to kill it on. You then just need to press the stop button and it should kill the process.

在 Eclipse 中,转到 DDMS 透视图,然后在设备选项卡中,在要杀死它的设备下单击要杀死的进程。然后你只需要按下停止按钮,它就会终止这个过程。

I'm not sure how you'd do this from the command line tool but there must be a way. Maybe you do it through the adb shell...

我不确定您如何从命令行工具执行此操作,但必须有一种方法。也许你是通过 adb shell 来做的...

回答by William A. Krithinithis Kripto

pkill NAMEofAPP

Non rooted marshmallow, termux & terminal emulator.

无根棉花糖、termux 和终端模拟器。