Android task killer

前端 未结 3 1246
心在旅途
心在旅途 2020-12-09 13:46

Im trying to write a simple task killer. I know I shouldnt kill tasks in Android, but Im eager to try something like this. I have the following code:

List<         


        
相关标签:
3条回答
  • 2020-12-09 14:24

    To achieve best result form killProcess , try calling killProcess many times.as

    for (int i = 0; i < 10; i++)
    {
     android.os.Process.killProcess(procInfo.get(i).pid);
    }
    

    NOTE:

    version 2.2 - killBackgroundProcesses : " This is the same as the kernel killing those processes to reclaim memory; the system will take care of restarting these processes in the future as needed."

    ActivityManager am = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
    am.killBackgroundProcesses("com.est.testetst");
    

    Required Permissions:

    android.Manifest.permission.KILL_BACKGROUND_PROCESSES
    

    version 2.1 - restartPackage -" This method is deprecated. (works for version less than 2.2. This is now just a wrapper for killBackgroundProcesses(String); the previous behavior here is no longer available to applications because it allows them to break other applications by removing their alarms, stopping their services, etc."

     ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        am.restartPackage(getPackageName());
    

    Required Permissions:

    <uses-permission android:name="android.permission.RESTART_PACKAGES" />
    
    0 讨论(0)
  • 2020-12-09 14:24

    Fortunately, killProcess() will only work for you to kill your own processes. You can tell this by reading the documentation.

    0 讨论(0)
  • 2020-12-09 14:43

    You can't kill other tasks that way because the Kernel will enforce permissions. Try this instead:

    ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
    activityManager.restartPackage(packageName);
    

    you will need the following permissions in the manifest:

    <uses-permission android:name="android.permission.RESTART_PACKAGES" />
    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
    

    EDIT:

    Actually restartPackage is deprecated. use killBackgroundProcesses() instead!

    0 讨论(0)
提交回复
热议问题