Stopping an Android app from console

前端 未结 11 663
情书的邮戳
情书的邮戳 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 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:

    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.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-27 09:56

    I tried all answers here on Linux nothing worked for debugging on unrooted device API Level 23, so i found an Alternative for debugging From Developer Options -> Apps section -> check Do Not keep activities that way when ever you put the app in background it gets killed

    P.S remember to uncheck it after you finished debugging

    0 讨论(0)
  • 2020-11-27 10: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.

    0 讨论(0)
  • 2020-11-27 10:01

    If all you are looking for is killing a package

    pkill package_name 
    

    should work

    0 讨论(0)
提交回复
热议问题