Stopping an Android app from console

前端 未结 11 718
情书的邮戳
情书的邮戳 2020-11-27 09:30

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 s

11条回答
  •  情话喂你
    2020-11-27 09:55

    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
    

提交回复
热议问题