Android进程杀手

发布于 2024-08-30 14:02:44 字数 211 浏览 4 评论 0原文

也许你能帮忙。

是否可以获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

决绝 2024-09-06 14:02:44

终止 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]:

Information you can retrieve about a
particular task that is currently
"running" in the system. Note that a
running task does not mean the given
task actual has a process it is
actively running in; it simply means
that the user has gone to it and never
closed it, but currently the system
may have killed its process and is
only holding on to its last state in
order to restart it when the user
returns.

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 ActivityManageris 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.

纸伞微斜 2024-09-06 14:02:44
    // Get currently running application processes
    List<ActivityManager.RunningAppProcessInfo> list = servMng.getRunningAppProcesses();
    if(list != null){
     for(int i=0;i<list.size();++i){
      if("com.android.email".matches(list.get(i).processName)){
       int pid = android.os.Process.getUidForName("com.android.email");
             android.os.Process.killProcess(pid);
      }else{
       mTextVIew.append(list.get(i).processName + "\n");
      }
     }
    }


    /*
    // Get currently running service
    List<ActivityManager.RunningServiceInfo> list = servMng.getRunningServices(1024);
    if(list != null){
     for(int i=0;i<list.size();++i){
      mTextVIew.append(list.get(i).service.getClassName() + "\n");
     }
    }
    */

    /*
    // Get currently running tasks
    List<ActivityManager.RunningTaskInfo> list = servMng.getRunningTasks(1024);
    if(list != null){
     for(int i=0;i<list.size();++i){
      mTextVIew.append(list.get(i).toString() + "\n");
     }
    }
    */
    // Get currently running application processes
    List<ActivityManager.RunningAppProcessInfo> list = servMng.getRunningAppProcesses();
    if(list != null){
     for(int i=0;i<list.size();++i){
      if("com.android.email".matches(list.get(i).processName)){
       int pid = android.os.Process.getUidForName("com.android.email");
             android.os.Process.killProcess(pid);
      }else{
       mTextVIew.append(list.get(i).processName + "\n");
      }
     }
    }


    /*
    // Get currently running service
    List<ActivityManager.RunningServiceInfo> list = servMng.getRunningServices(1024);
    if(list != null){
     for(int i=0;i<list.size();++i){
      mTextVIew.append(list.get(i).service.getClassName() + "\n");
     }
    }
    */

    /*
    // Get currently running tasks
    List<ActivityManager.RunningTaskInfo> list = servMng.getRunningTasks(1024);
    if(list != null){
     for(int i=0;i<list.size();++i){
      mTextVIew.append(list.get(i).toString() + "\n");
     }
    }
    */
难得心□动 2024-09-06 14:02:44

至少可以说,通过 Java 退出 Android 应用程序是很棘手的。由于上面列出的所有原因,通过 Process Object 接口杀死应用程序通常被认为是不好的形式,我不会重复它们,但我不认为发布的劝阻是为了阻止你一起做这一切,它们只是让你了解可能的警告。问题是,Android 必须跟踪 Activity 和加载/运行顺序,因为设备有一个后退按钮...所以您尽职尽责地调用 finish()finishFromChild(childActivity),当用户退出应用程序时,您的应用程序中存在先前的 Activity,很乐意忽略要求其退出的代码。

有时,您只需杀死该应用程序即可。所以......在进行数字化流程杀戮之前,您必须做一些计划。确保用户不会终止对您的应用至关重要的任何文件 I/O 或网络通信。我的策略是前端加载我的 Activity,以完成加载事件或加载事件附近的所有繁重工作。

另外,我总是在用户退出之前提示用户,这是一种桌面应用程序 UI 约定,在这里可以很好地翻译。

上面的代码通过调用“getUidForName”错过了标记...

此示例从硬件后退按钮捕获 Android Native 后退按钮事件,并提示用户“你真的吗?”想要离开我的应用程序”,如果用户选择“是”,则会杀死该应用程序。

    @Override 
public boolean onKeyDown(int keyCode, KeyEvent event) {
    //Handle the back button
    if(keyCode == KeyEvent.KEYCODE_BACK) {
        //Ask the user if they want to quit
        new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.exclamationpoint)
        .setTitle("Exit?")
        .setMessage("You are about to exit the Application. " + 
                     "Do you really want to exit?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
             @Override
            public void onClick(DialogInterface dialog, int which) {
                //Stop the activity
                 //maintenancetabs.this.finish();
                 int pid = android.os.Process.myPid();
                 android.os.Process.killProcess(pid);
                }
         })
        .setNegativeButton("No", null)
        .show();
         return true;
    }     else {
        return super.onKeyDown(keyCode, event);
    }
 }

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 a back button...so you dutifully call finish() or finishFromChild(childActivity)and when the user exits 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.

    @Override 
public boolean onKeyDown(int keyCode, KeyEvent event) {
    //Handle the back button
    if(keyCode == KeyEvent.KEYCODE_BACK) {
        //Ask the user if they want to quit
        new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.exclamationpoint)
        .setTitle("Exit?")
        .setMessage("You are about to exit the Application. " + 
                     "Do you really want to exit?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
             @Override
            public void onClick(DialogInterface dialog, int which) {
                //Stop the activity
                 //maintenancetabs.this.finish();
                 int pid = android.os.Process.myPid();
                 android.os.Process.killProcess(pid);
                }
         })
        .setNegativeButton("No", null)
        .show();
         return true;
    }     else {
        return super.onKeyDown(keyCode, event);
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文