java 如何在 Android 中关闭另一个应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3773516/
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-10-30 03:18:37  来源:igfitidea点击:

How to close another app in Android?

javaandroid

提问by Jimmy

I have been developing an app, and I need to close another app in my code. Does anyone know any api to call to close an app?

我一直在开发一个应用程序,我需要在我的代码中关闭另一个应用程序。有谁知道可以调用关闭应用程序的任何 API?

BTW: my app will be pre-installed.

顺便说一句:我的应用程序将被预装。

thanks

谢谢

回答by Nick

Since Android 2.2(i.e. going forward), you can only close the background processesof other apps, you are no longer able to close their main activities.

由于Android 2.2(即前进),您只能关闭background processes其他应用程序的,您不再能够关闭它们的主要活动。

If your app is targeting Android <2.2, look atandroid.permission.RESTART_PACKAGE.

如果您的应用面向 Android <2.2,请查看android.permission.RESTART_PACKAGE.

If you want it to work properly on 2.2 and above (which you should :-)), look at android.permission.KILL_BACKGROUND_PROCESSES, but again, this only closes background services and such and might "mess up" the other app rather than doing any good.

如果您希望它在 2.2 及更高版本上正常工作(您应该:-)),请查看android.permission.KILL_BACKGROUND_PROCESSES,但同样,这只会关闭后台服务等,并且可能会“弄乱”其他应用程序而不是做任何好事。

With the right permissions, you can then do the following:

使用正确的权限,您可以执行以下操作:

private ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
am.restartPackage("com.jimmy.appToBeClosed");

回答by Ahmed

Try This

试试这个

    ActivityManager am = (ActivityManager) getApplicationContext().getSystemService("activity");
    Method forceStopPackage;                
    forceStopPackage =am.getClass().getDeclaredMethod("forceStopPackage",String.class);
    forceStopPackage.setAccessible(true);
    forceStopPackage.invoke(am, pkg);

In manifest file add this

在清单文件中添加这个

<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"></uses-permission>

回答by poe

If both applications are yours, you can use AIDL for inter-process communication to send a message telling the other application to close. See http://developer.android.com/guide/developing/tools/aidl.html.

如果这两个应用程序都是你的,你可以使用 AIDL 进行进程间通信,以发送消息告诉另一个应用程序关闭。请参阅http://developer.android.com/guide/developing/tools/aidl.html

回答by Android Dev Dude

You don't ever really want to close another application, due to Android activity lifecycle.
There's no benefit, and always detriment to closing another app if it's not yours, and very little benefit to closing your own.

由于 Android 活动生命周期,您真的不想关闭另一个应用程序。
没有任何好处,如果另一个应用程序不是你的,那么关闭它总是有害的,关闭你自己的应用程序几乎没有好处。

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

If you know for certain that you'll never, ever need a root activity and its children (an "app"), you can stop it to free memory (it doesn't free that much), but if you do the user may restart it while it's still in cache, stopped, which can cause problems if the stopped state is restored. So this is a bad practice.

如果您确定您永远不会需要根活动及其子活动(“应用程序”),您可以停止它以释放内存(它不会释放那么多),但如果您这样做,用户可能会在它仍在缓存中时重新启动它,停止,如果停止状态恢复,这可能会导致问题。所以这是一个不好的做法。