如何关闭所有后台活动应用程序?

时间:2014-02-18 11:36:09

标签: android

我有一个要求,我希望关闭所有后台运行的应用程序。我使用了这段代码,但它不起作用......

    ActivityManager  manager =  (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> listOfProcesses = manager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo process : listOfProcesses)
    {
        System.out.println(process.processName);
        manager.killBackgroundProcesses(process.processName);
    }

任何帮助?

2 个答案:

答案 0 :(得分:0)

您只能杀死自己的应用,例如在同一进程中运行或具有相同userID的那些。除非设备已植根,否则您无法杀死他人。

看看这个答案。 How to kill currently running task in android

您的问题现在将得到解决

答案 1 :(得分:0)

试试这个

List<ApplicationInfo> packages;
    PackageManager pm;
    pm = getPackageManager();
    // get a list of installed apps.
    packages = pm.getInstalledApplications(0);

    ActivityManager mActivityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);

    for (ApplicationInfo packageInfo : packages) {
        if ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1)
            continue;
        if (packageInfo.packageName.equals("mypackage"))
            continue;
        mActivityManager.killBackgroundProcesses(packageInfo.packageName);
    }