如何杀死一个第三方的应用程序? [英] How to kill a 3rd-party application?

查看:215
本文介绍了如何杀死一个第三方的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序需要杀一个特定的应用程序。是否有可能A股,无根设备上?如果是的话 - 如何?我知道它的进程名和PID。

My program needs to kill a specific application. Is it possible on a stock, unrooted device? If yes - how? I know its process name and PID.

推荐答案

再一次,我回答为时已晚,但因为我在同样的情况现在来看,我认为,以帮助别人分享我的发现。 首先你需要了解什么,你可以杀了,什么不可以的。通过看Android的点的应用程序并不像其他操作系统。一个Android应用程序包含许多组件(活动,广播接收器,服务,最重要的任务等),它们装在一个包。包可以有更多的一个进程中运行依赖于运行其组件。现在,有趣的是,Android包不被认为(由机器人)封杀或停止,如果它的任何过程的部分或全部杀死,其实包仍可甚至没有任何进程在运行,在所有运行。如果你开始一个模拟器启动程序(IE浏览器),然后再杀死它通过DDMS过程中,你可以看到这个效果,之后去到应用程序的包设置(设置 - >应用程序 - >管理应用程序 - >全部 - >浏览器),你可以看到强制停机按钮启用,这意味着应用程序仍在运行(但从Android的点)。这里发生的事情是,应用程序有一个或多个任务冻结。也就是说,Android已经保存的应用程序的活动(任务或任务)等封装仍在运行,或者如果用户返回到它,他将降落在他做的最后一件事更好的状态。现在,如果你点击强制停止按钮,Android将放弃所有这些冻结任务,当用户返回到应用程序,他将看到的第一个活动。一个任务是有系统的同一个键可以做到这一点(也许根功能的应用程序签名的东西,你不能杀(因为升级Froyo)只有用户(从强制停止按钮),系统或第三方应用程序但我还没有证实这一点)。另外一个方法是你可以杀死并回收其使用,只要内存为您遵循一些限制:

Once again I am answering too late but since I run on a same situation today I thought to share my findings in order to help someone. First of all you need to understand what you can kill and what not. By android's point of view an application is not like other OSes. An android application consists of many components (activities, broadcast receivers, services, most important tasks etc) which are packed in a package. A package can have more that one processes running depending on its components running. Now the interesting part is that an android package isn't considered (by android) "killed" or "stopped" if any or all of its processes have killed, in fact a package can still running even with no processes running at all. You can see this effect if you start an emulator start a program (i.e. Browser) and then kill its process via DDMS, after that go to the application's package settings (Settings --> Applications --> Manage Applications --> All --> Browser), you can see the "Force Stop" button enabled, this means that the application is still running (from android's point of view). What happened here is that the application has one or more tasks "frozen". That is, android has saved the state of the application's activities (task or tasks) and so the package is still running or better if the user returns to it he will land on the last thing he was doing. Now if you click the "Force Stop" button, android will discard all of these "frozen" tasks and when the user returns to the application he will see the first activity. A Task is something you cannot kill (since froyo) only the user (from "Force Stop" button), the system or a third party application which is signed with the same key of the system can do that (and maybe a root capable application but I have not confirmed this). On the other hand a Process is something you can kill and reclaim the memory it uses, as long as you follow some restrictions:

  1. 您有android.permission.KILL_BACKGROUND_PROCESSES权限。
  2. 的处理并非系统或根进程。
  3. 的过程不属于组分是持久的。
  4. 的方法不是关键的,以便系统通过任何其他方式进行操作。

除了1号规则,你不必做一些关于他们,Android将照顾这。

Besides the no 1 rule you do not have to do something about them, android will take care of this.

ActivityManager有一个方便的功能,您可以使用,以杀死所有包有一次的过程。当你调用它的Andr​​oid会杀死任何进程会被杀死,从而释放一些内存。然而此包任务的状态将被保存,当用户返回到应用程序,他会看到他在做什么,除非系统本身已经杀死了他们的最后一件事。这可以发生,因为它需要的资源或状态保存很久以前(约30分钟)。的副作用是,由于用户之所想,所有的应用程序都像在桌面操作系统,他们不相信该应用程序是真正关闭,但是这是与Android的使用寿命。

ActivityManager has a handy function you can use in order to kill all of the processes a package has at once. When you invoke it android will kill any process can be killed and thus freeing up some memory. However the state of the tasks for this package will be saved and when the user returns to the application he will see the last thing he was doing unless the system itself has killed them. This can occur either because it needs resources or the state was saved long time ago (about 30 minutes). The side-effect is that because users are thinking that all applications are like in desktop operating systems, they do not believe that the application is really closed but this is the life with android.

现在的code:

有关我的项目,我有prepared三个函数来实现这一点。

For my project I have prepared three functions to achieve this.

第一个查找第一个过程PID包可能有它返回-1,如果没有任何。

The first one looks for the first process pid a package may have and it returns -1 if there aren't any.

private Context cx;
private ActivityManager am = (ActivityManager) cx.getSystemService(Context.ACTIVITY_SERVICE);

public int findPIDbyPackageName(String packagename) {
    int result = -1;

    if (am != null) {
        for (RunningAppProcessInfo pi : am.getRunningAppProcesses()){
            if (pi.processName.equalsIgnoreCase(packagename)) {
                result = pi.pid;
            }
            if (result != -1) break;
        }
    } else {
        result = -1;
    }

    return result;
}

第二个做一些愚蠢的事,但我需要它为我的项目。

The second does something stupid but I need it for my project.

public boolean isPackageRunning(String packagename) {
    return findPIDbyPackageName(packagename) != -1;
}

第三个做这项工作。

The third does the job.

public boolean killPackageProcesses(String packagename) {
    boolean result = false;

    if (am != null) {
        am.killBackgroundProcesses(packagename);
        result = !isPackageRunning(packagename);
    } else {
        result = false;
    }

    return result;
}

他们证实与仿真器API 8,9和实际设备(GALAXY S2)与API 15上工作,他们尽杀任何应用程序的进程(不仅仅是你自己的),只要是不需要的。

They are confirmed to work with emulator API 8 and 9 and on a real device (Galaxy S2) with API 15 and they DO kill any application's processes (not just your own) as long as they aren't needed.

现在关于android.os.Process.killProcess文档该条规定:

Now about the android.os.Process.killProcess documentation which states that:

...通常,这意味着只能在运行调用者的包/应用程序,并通过该应用程序创建的任何其他进程的进程; ......

... Typically this means only the process running the caller's packages/application and any additional processes created by that app; ...

我相信(你可能注意到了我的英语不够好),通过运行调用者的包/应用程序的进程是指家庭启动应用程序,而不是你自己的应用程序。您的应用程序是来电运行呼叫者的包/应用程序的过程是家庭发射器或任何其他应用程序,启动应用程序。这是对我解释说,killBackgroundProcesses功能和android.os.Process.killProcess功能确实是工作的第三方应用程序的唯一途径。

I believe (as you may noticed my English aren't good enough) that by "the process running the caller's packages/application" means the home launcher application and NOT your own application. Your application is the "caller" and the process running the "caller's packages/application" is the home launcher or any other app launched your application. This is the only way for me to explain that the killBackgroundProcesses function and the android.os.Process.killProcess function are indeed work on third party applications.

这篇关于如何杀死一个第三方的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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