4

I am using eclipse. I am trying to kill a process in my application. However, in eclipse it does not seem to know the hint for killBackgroundProcess from the ActivityManager and it will not let me proceed. I read that you have to have permissions to kill background processes and already added the permission which it did not recognize either from the manifest. Here is the code that I am trying to use:

ActivityManager activityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcess(myProcessId);
tshepang
  • 12,111
  • 21
  • 91
  • 136
ngreenwood6
  • 8,108
  • 11
  • 33
  • 52
  • 3
    The method signature is "public void killBackgroundProcesses (String packageName)" . Not "public void killBackgroundProcess (int processID)" . – gtiwari333 Oct 14 '11 at 12:05

5 Answers5

3

Make sure you are targeting API level 8, as that method was only added in Android 2.2.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2

The method signature is

public void killBackgroundProcesses (String packageName)

Not

public void killBackgroundProcess (int processID)

The following is working CODE :

public static void killThisPackageIfRunning(final Context context, String packageName){
    ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    activityManager.killBackgroundProcesses(packageName);
}   

But this works only for >= API Level 8 as this method was added in Android 2.2.

gtiwari333
  • 24,554
  • 15
  • 75
  • 102
0

Be sure that your calling killBackgroundProcesses with the topmost Activity.

kyildizoglu
  • 97
  • 2
  • 7
0

You can try these codes below, it works for me .

public static void clearMemory(Context context) {
        ActivityManager activityManger = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> list = activityManger.getRunningAppProcesses();
        if (list != null)
            for (int i = 0; i < list.size(); i++) {
                ActivityManager.RunningAppProcessInfo apinfo = list.get(i);

                String[] pkgList = apinfo.pkgList;

                if (apinfo.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE ) {
                    for (int j = 0; j < pkgList.length; j++) {
                        activityManger.killBackgroundProcesses(pkgList[j]);
                    }
                }
            }
    }
SadieYu
  • 7
  • 2
0

The argument is package Name not processId. Try passing it something like:

myActivity.getApplication().getPackageName()

as an argument.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
james
  • 11
  • 1