2

The question above basically explains my question. How does Advanced Task Killer kill an application ?

I have tried the method mentioned in the seconds answer to this question. Which is using ActivityManager.killBackgroundProcesses.

for(RunningAppProcessInfo runningProcess : runningProcesses){
                    for(ApplicationInfo nonCompliantApp : nonCompliantApps){
                        if(runningProcess.pkgList != null) for(String runningAppPackageName : runningProcess.pkgList){
    //                      if(runningProcess.processName.startsWith("gameapps.avatar.")){
    ////                            FileManager.writeToLogFile(SystemApplicationManager.class, "run", LogMessageType.DEBUG, "Game Apps: " + runningProcess.processName + "  pkg: " + runningProcess.pkgList.length + " - " + runningProcess.pkgList[0]);
    //                      }

                            if(runningAppPackageName.equals(nonCompliantApp.packageName)){
                                ActivityManager amgr = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
                                ShellCommandManager.executeShellCommand("kill " + runningProcess.pid);
                            amgr.killBackgroundProcesses(nonCompliantApp.packageName);
                                FileManager.writeToLogFile(SystemApplicationManager.class, "run", LogMessageType.DEBUG, "Process found and killed: " + nonCompliantApp.packageName);
                            }

                            if(!keepRunning)break;
                        }
                    }

The problem is, after executing the above code, I check under settings -> apps -> running , only to find that the same application is still running. The device I am testing this on is an HTC One X running Android ICS.

What am I doing wrong ? Some help or clarification would be greatly appreciated.

Community
  • 1
  • 1
Heshan Perera
  • 4,592
  • 8
  • 44
  • 57
  • You could look at http://code.google.com/p/freetaskmanager/source/browse/ It seems to do what you're asking. – Austin Jun 14 '12 at 06:34

2 Answers2

1

It gets the application which are running and uses its process id to kill the running process. a small piece of code might help you.

int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
Rakshi
  • 6,796
  • 3
  • 25
  • 46
  • Thanks. But, doing the above would simply kill my own process right ? I mean, the pid you pass into the Process.killProcess method is the id of the process that is being killed right ? – Heshan Perera Jun 14 '12 at 06:48
  • Entering the PID there does not work. It only allows you to kill processes which belong to the package of your process or other processes that were launched by your process. The kernel prevents you from doing AFAIK. Anyway I tried this approach before and it didn't work. – Heshan Perera Jun 14 '12 at 08:21
0

Try this:

private static void killProcess(String process_name)
{
     ActivityManager  manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
     List<ActivityManager.RunningAppProcessInfo> listOfProcesses = manager.getRunningAppProcesses();

     for (ActivityManager.RunningAppProcessInfo process : listOfProcesses)
     {
         if (process.processName.equals(process_name))
         {

                 manager.restartPackage(process.pkgList[0]);
                 Log.e(TAG, "Killed process [" + process.pkgList[0] + "]"); 

         }
     }
}
edoardotognoni
  • 2,752
  • 3
  • 22
  • 31
  • Please explain how this answers the question – CocoNess Oct 04 '12 at 19:26
  • If the first code doesn't kill a process, this one, could be the right one. I don't know how Advanced Task Killer kills process, do you? I didn't write that app. Anyway, if this code works, could be the answer for the question. But i think that no one can give the right answer, unless he comes from the Advanced Task Killer's team. Don't you agree? – edoardotognoni Oct 05 '12 at 09:17