如何从包名中获取PID?

时间:2014-08-13 21:51:29

标签: android

我需要创建一个函数,但我不知道如何获取应用程序的PID。

// here return -1 but I need PID to kill process
Integer PID1= android.os.Process.getUidForName("com.android.email");

android.os.Process.killProcess(PID1);

1 个答案:

答案 0 :(得分:2)

restartPackage代码

ActivityManager aM = (ActivityManager);
getApplicationContext().getSystemService(getApplicationContext().ACTIVITY_SERVICE);
aM.restartPackage("com.android.email");

杀死BackGround流程代码

ActivityManager aM = (ActivityManager)
getApplicationContext().getSystemService(Catalogue.content_holder.getApplicationContext().ACTIVITY_SERVICE);
aM.killBackgroundProcesses("com.android.email");

以下是获取所有正在运行的应用程序的代码,并检查其中的电子邮件应用程序是否已经运行,如果它正在运行,则会终止该进程

ActivityManager manager =  (ActivityManager) getApplicationContext.getSystemService(getApplicationContext.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> activityes = ((ActivityManager)manager).getRunningAppProcesses();

for (int iCnt = 0; iCnt < activityes.size(); iCnt++){

    System.out.println("APP: "+iCnt +" "+ activityes.get(iCnt).processName);

    if (activityes.get(iCnt).processName.contains("com.android.email")){
        android.os.Process.sendSignal(activityes.get(iCnt).pid, android.os.Process.SIGNAL_KILL);
        android.os.Process.killProcess(activityes.get(i).pid);
        //manager.killBackgroundProcesses("com.android.email");

        //manager.restartPackage("com.android.email");

        System.out.println("Inside if");
    }

}