以编程方式强制使用Java停止Android应用程序?
问题说明
如何强制使用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" />
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcebfec
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20 -
希伯来语的空格句子标记化错误
it1352 06-22