如何强制停止Android应用 [英] How to force stop an Android app

查看:285
本文介绍了如何强制停止Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Java代码强制停止应用程序?我正在尝试构建一个内存清理器,它可以帮助清理后台进程。
强制停止的意思是确保当你转到应用程序详细信息时,强制停止按钮显示为灰色。
我知道有一种方法可以杀死应用程序的进程,但是当你进入运行列表时,即使你杀了它,应用程序仍然存在。我已经尝试了很多类似的内存清理应用程序,其中只有一个可以完全 FORCE STOP 应用程序,但它有很多无用的通知 - 非常烦人。

How to force stop an app using Java code? I'm trying to build a memory cleaner, that can help clean out background processes. What I mean by force stop is to make sure when you go to the app details, the FORCE STOP button is grey out. 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.

PS:
当你去设置 - >应用,您可以看到应用列表,点击其中一个,您可以访问它的应用信息。有一个名为 FORCE STOP 的按钮。通过点击它,应用程序完全被杀死。我想在我的应用中执行这种操作,是否有任何代码片段可以做到这一点?

PS: When you go to Settings -> Apps, you can see a list of apps, click on one of them, you can get to the App info of it. There is a button named FORCE STOP. By clicking on it, the app is totally killed. I want to perform that kind of action in my app, is there any code snippet to do that?

推荐答案

获取应用程序的进程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(PackageName)

之前发送信号。

请注意,您的应用需要拥有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" />

这篇关于如何强制停止Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆