如何以编程方式强制使用Java停止Android应用程序? [英] How can I programmatically force stop an Android app with Java?
问题描述
如何强制使用Java停止应用程序?我正在尝试构建一个内存清理器,以帮助清理后台进程.
How can I force stop an app with Java? I'm trying to build a memory cleaner that can help clean out background processes.
我知道有一种方法可以终止应用程序的进程,但是当您进入运行列表时,即使终止了该应用程序,该应用程序仍然存在.而且我尝试了很多类似的内存清理应用程序,其中只有一个可以完全强制停止应用程序,但是它有那么多无用的通知-非常烦人.
I know there is a way to kill the process of an app, but when you go to the running list, the app is still there even after you have killed it. And I have tried a lot of similar memory cleaning apps, only one of them can totally force stop apps but it has so many useless notifications - very annoying.
P.S .:当您转到设置"->应用程序"时,将看到一个应用程序列表.单击这些应用程序之一,您最终将获得该应用程序的信息.有一个名为强制停止" 的按钮.通过单击它,该应用程序将被杀死.我想在我的应用中执行这种操作.该怎么办?
P.S.: When you go to Settings -> Apps, you will see a list of apps. Click on one of these apps and you end up on the app's info. There is a button named "force stop". By clicking on it, the app is killed. I want to perform that kind of action in my app. How can this be done?
推荐答案
获取应用程序的进程ID,并终止该进程的onDestroy()方法
get the process ID of your application, and kill that process onDestroy() method
@Override
public void onDestroy()
{
super.onDestroy();
int id= android.os.Process.myPid();
android.os.Process.killProcess(id);
}
或
getActivity().finish();
System.exit(0);
如果您想从活动中杀死其他应用程序,则应该可以使用
and if you want to kill other apps from your activity, then this should work
您可以使用以下方式发送信号:
You can send the signal using:
Process.sendSignal(pid, Process.SIGNAL_KILL);
要完全终止该进程,建议致电:
To completely kill the process, it's recommended to call:
ActivityManager.killBackgroundProcesses(packageNameToKill)
在发送信号之前.
请注意,您的应用需要拥有KILL_BACKGROUND_PROCESSES权限.因此,在AndroidManifest.xml中,您需要包括:
Please, note that your app needs to own the KILL_BACKGROUND_PROCESSES permission. Thus, in the AndroidManifest.xml, you need to include:
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
这篇关于如何以编程方式强制使用Java停止Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!