• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

以编程方式强制使用Java停止Android应用程序?

用户头像
it1352
帮助1

问题说明

如何强制使用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?

正确答案

#1

获取应用程序的进程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" />

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcebfec
系列文章
更多 icon
同类精品
更多 icon
继续加载