2

上面的问题基本上解释了我的问题。Advanced Task Killer 如何杀死应用程序?

我已经尝试过这个问题的第二个答案中提到的方法。哪个正在使用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;
                        }
                    }

问题是,执行上述代码后,我在 settings -> apps -> running 下查看,却发现同一个应用程序仍在运行。我正在测试的设备是运行 Android ICS 的 HTC One X。

我究竟做错了什么 ?一些帮助或澄清将不胜感激。

4

2 回答 2

1

它获取正在运行的应用程序并使用其进程 ID 来终止正在运行的进程。一小段代码可能会对您有所帮助。

int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
于 2012-06-14T06:38:20.370 回答
0

尝试这个:

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] + "]"); 

         }
     }
}
于 2012-10-04T07:14:00.167 回答