App有一些信息是放在PM的结构体里面的,比如ApplicationInfo,
包含了应用程序名字;而一些信息是封在AM的结构体里面的,比如RunningAppProcessInfo,包含了应用的进程名等,
包含了应用程序名字;而一些信息是封在AM的结构体里面的,比如RunningAppProcessInfo,包含了应用的进程名等,
下的函数描述了如何由RunningAppProcessInfo得到对应的ApplicationInfo。
private ApplicationInfo getApplicationInfo(ActivityManager.RunningAppProcessInfo info, int uid) {
// 1. try to get packageName from runningProcessInfo
try {
if (info != null) {
String[] pkgs = info.pkgList;
for (int i = 0; i < pkgs.length; i++) {
ApplicationInfo ai = mPm.getApplicationInfo(pkgs[i],
PackageManager.GET_UNINSTALLED_PACKAGES);
if (ai.uid == uid) {
return ai;
}
}
}
} catch (PackageManager.NameNotFoundException e) {
}
// 2. try to use processName as packageName
try {
ApplicationInfo ai = mPm.getApplicationInfo(info.processName,
PackageManager.GET_UNINSTALLED_PACKAGES);
if (ai.uid == uid) {
return ai;
}
} catch (PackageManager.NameNotFoundException e) {
}
// 3. If we couldn't get information about the overall
// process, try to find something about the uid.
String[] pkgs = mPm.getPackagesForUid(uid);
// 3.1 If there is one package with this uid, that is what we want.
if (pkgs.length == 1) {
try {
ApplicationInfo ai = mPm.getApplicationInfo(pkgs[0],
PackageManager.GET_UNINSTALLED_PACKAGES);
return ai;
} catch (PackageManager.NameNotFoundException e) {
}
}
// 3.2 If there are multiple, see if one gives us the official name
// for this uid.
for (String name : pkgs) {
try {
PackageInfo pi = mPm.getPackageInfo(name, 0);
if (pi.sharedUserLabel != 0) {
CharSequence nm = mPm.getText(name,
pi.sharedUserLabel, pi.applicationInfo);
if (nm != null) {
return pi.applicationInfo;
}
}
} catch (PackageManager.NameNotFoundException e) {
}
}
// 4. Finally... whatever, just pick the first package's name.
try {
ApplicationInfo ai = mPm.getApplicationInfo(pkgs[0],
PackageManager.GET_UNINSTALLED_PACKAGES);
return ai;
} catch (PackageManager.NameNotFoundException e) {
}
return null;
}