Stopping an Android app from console

2019-01-05 07:29发布

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.

10条回答
家丑人穷心不美
2楼-- · 2019-01-05 07:47

If all you are looking for is killing a package

pkill package_name 

should work

查看更多
小情绪 Triste *
3楼-- · 2019-01-05 07:48

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.

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 shell am broadcast -a com.example.TestActivity.STOP
查看更多
别忘想泡老子
4楼-- · 2019-01-05 07:49

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

have fun!

查看更多
爷、活的狠高调
5楼-- · 2019-01-05 08:00

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

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

adb shell am kill com.your.package
查看更多
对你真心纯属浪费
6楼-- · 2019-01-05 08:01

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

查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-05 08:03

Edit: 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.

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.


If you're on 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 su beforehand.

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

查看更多
登录 后发表回答