2011-09-17 86 views

回答

87

由於每個應用程序都有自己的進程ID,可以通過

int pid = android.os.Process.myPid(); 
17

得到它這也適用於:

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses(); 
int processid = 0; 
for (int i = 0; i < pids.size(); i++) { 
    ActivityManager.RunningAppProcessInfo info = pids.get(i); 
    if (info.processName.equalsIgnoreCase("here your package name")) { 
     processid = info.pid; 
    } 
} 
相關問題