Android进程杀手
也许你能帮忙。
是否可以获取 Android 系统中正在运行的所有进程的列表,并杀死其中的一些进程?我知道有一些应用程序(任务管理器
),但我想编写自己的简单应用程序。
我想编写简单的任务管理器,只列出所有进程和按钮,这将杀死其中的一些进程。
你能写一些我可以调用的Java方法来获取进程列表以及杀死它们的方法吗?或者只是给我一些建议。
Maybe you can help.
Is it possible to get list of all Processes
which are running in the Android system, and kill some of them? I know that there are some applications (task managers
), but I would like to write my own, simple application.
I would like to write simple task manager, just list of all processes and button which will kill some of them.
Could you just write some Java methods which I can call in order to get list of process, and method for killing them. Or just give me some advice's.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
终止 Android 中的应用/服务通常是一个非常糟糕的主意。虽然可以编写任务杀手应用程序,但不应鼓励将其用于开发/调试目的之外的任何用途。
任务管理是 Android O/S 的职责,你看到的任务不是进程(例如你在 Windows 任务管理器中看到的进程),事实上,它们只是当 Android 告诉他们可以拥有一个流程时,就拥有一个流程。
应用程序经常被这些任务管理工具破坏,因为它们通常无法从强制终止中恢复,特别是当它们被杀死时正忙于写入文件或使用其他资源时。它还会让手机用户产生一种错误的期望,即列出的应用程序实际上在他们的手机上运行,但事实往往并非如此。 [ActivityManager 文档][1] 对此进行了解释:
当您在 TaskKiller 或 Quick System Info 等应用程序中看到
正在运行
应用程序列表时,其中许多应用程序实际上并未运行,它们只是处于挂起状态。这些应用程序不会消耗系统资源,因为 Android 已决定停止它们,直到再次需要它们为止。然而,当你杀死它们时,你没有给它们时间彻底关闭,当你下次尝试启动它们时,你可能会看到一个不友好的强制关闭对话框。我见过应用程序完全崩溃,甚至重新安装也无效,因为它们试图读取 SD 卡上损坏的文件,或者使用非官方 API 调用。总之,朋友们不要让朋友们在Android中使用任务杀手。
无论如何,为了回答您的问题,大多数应用程序使用
ActivityManager
来列出处于运行/挂起状态的活动。freetaskmanager 是这些
任务管理器
使用中。Killing apps/services in Android is generally a really bad idea. Whilst it is possible to write
task killer
apps, it shouldn't be encouraged for anything outside of development/debugging purposes.Task management is the responsibility of the Android O/S, the tasks you can see are not processes (in the sense of the processes you see in the Windows task manager for example), in fact, they only have a process when Android tells them they can have one.
Apps are regularly broken by these task management tools, as they often fail to recover from the forced termination, particularly if they were busy writing to a file or using another resource when they were killed. It also puts the handset users into a false expectation that the apps listed are actually RUNNING on their phone, which they are often not. This is explained in the [ActivityManager docs][1]:
When you see the list of
running
apps in apps like TaskKiller or Quick System Info, many of them are not actually running, they are just in a suspended state. These apps are not consuming system resources because Android has decided to stop them until they are needed again. However, when you kill them, you don't give them time to shut down cleanly, and when you try to launch them next time you can be presented with an unfriendly force close dialog. I have seen apps break completely, with even a re-install being ineffective, because they are trying to read a corrupted file on the SD card, or they use unofficial API calls.In short, friends don't let friends use task killers in Android.
Anyway, to answer your question, the
ActivityManager
is what most of these apps use to list activities that are in running/suspended state.freetaskmanager is an example of one of these
task managers
in use.至少可以说,通过 Java 退出 Android 应用程序是很棘手的。由于上面列出的所有原因,通过 Process Object 接口杀死应用程序通常被认为是不好的形式,我不会重复它们,但我不认为发布的劝阻是为了阻止你一起做这一切,它们只是让你了解可能的警告。问题是,Android 必须跟踪 Activity 和
加载/运行顺序
,因为设备有一个后退
按钮...所以您尽职尽责地调用finish()
或finishFromChild(childActivity)
,当用户退出
应用程序时,您的应用程序中存在先前的 Activity,很乐意忽略要求其退出的代码。有时,您只需杀死该应用程序即可。所以......在进行数字化流程杀戮之前,您必须做一些计划。确保用户不会终止对您的应用至关重要的任何文件 I/O 或网络通信。我的策略是前端加载我的 Activity,以完成加载事件或加载事件附近的所有繁重工作。
另外,我总是在用户退出之前提示用户,这是一种桌面应用程序 UI 约定,在这里可以很好地翻译。
上面的代码通过调用“getUidForName”错过了标记...
此示例从硬件后退按钮捕获 Android Native
后退按钮
事件,并提示用户“你真的吗?”想要离开我的应用程序”,如果用户选择“是”,则会杀死该应用程序。Exiting an Android App via Java is tricky to say the least. Killing the app via the Process Object interface is generally considered bad form for all the reasons listed above, I wont rehash them, but I don't feel that the published discouragements are meant to stop you from doing it all together, they just make you aware of possible caveats. Here's the catch, Android has to keep track of Activities and
load/run order
because the devices have aback
button...so you dutifully callfinish()
orfinishFromChild(childActivity)
and when the userexits
the app, there's a previous activity from your app, happily ignoring your code that asked it exit.Sometimes, you just have to kill the app. So...before committing this digital process kill, you have to do a little planning. Make sure the user won't be killing any file I/O or network communication vital to your app. My strategy for this was to
front load
my Activities to do all the heavy lifting on or near to the load events.Also, I always prompt the user before they exit, a desktop application UI convention that translates well here.
The above code misses the mark by calling "getUidForName"...
This example captures the Android Native
back button
event from the hardware back button and prompts the user "do you really want to leave my app" and if the user selects "yes" it kills the App.