从RunningAppProcessInfo得到ApplicationInfo

本文详细阐述了在Android系统中通过Activity Manager (AM) 和 Package Manager (PM) 结构体获取应用程序信息的方法,包括从RunningAppProcessInfo、ApplicationInfo等结构体中获取应用程序的名字、进程名等关键信息。
App有一些信息是放在PM的结构体里面的,比如ApplicationInfo,
包含了应用程序名字;而一些信息是封在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;
}


### ApplicationInfo in Android Development #### Overview of ApplicationInfo In the context of Android development, `ApplicationInfo` represents metadata about an application package that is retrieved from the manifest file. This object contains various properties and flags which provide detailed information regarding how the application should operate within the system environment[^1]. The following attributes are part of this class: - **packageName**: The name of the package. - **sourceDir**: Absolute path to the .apk file on disk. - **publicSourceDir**: Public source directories (for split APKs). - **dataDir**: Directory where persistent data can be stored by the app. - **nativeLibraryDir**: Path to native libraries directory. Additionally, several boolean fields indicate specific characteristics or capabilities such as whether it has code (`hasCode`) or supports small screens (`supportsSmallScreens`). There also exist methods like `loadLabel()`, allowing developers to access human-readable labels defined inside `<application>` tags. #### Retrieving Information Using PackageManager Developers often interact with instances of `ApplicationInfo` via the `PackageManager`. By calling appropriate APIs provided by `PackageManager`, one can obtain details concerning installed applications including their icons, names, permissions required, etc., all encapsulated within objects derived from `PackageItemInfo`. For instance, when querying for packages using `getInstalledApplications(int flags)` method, each result will contain its corresponding `ApplicationInfo` structure filled out based upon what was specified under respective `<application>` elements found during parsing process conducted over `AndroidManifest.xml`. ```java // Example Code Snippet Showing How To Get An Instance Of ApplicationInfo From Package Name try { String targetPackageName = "com.example.app"; Context context = getApplicationContext(); PackageManager pm = context.getPackageManager(); // Retrieve Application Info With Specific Flags Applied For More Detailed Data int flagValue = PackageManager.GET_META_DATA; ApplicationInfo ai = pm.getApplicationInfo(targetPackageName, flagValue); } catch (NameNotFoundException e) { Log.e("Error", "Failed to find info"); } ``` --related questions-- 1. What other classes extend from PackageItemInfo besides ApplicationInfo? 2. Can you explain more about the role of PackageManager in managing apps' lifecycle events? 3. In what scenarios would someone need to use GET_UNINSTALLED_PACKAGES flag while fetching ApplicationInfo? 4. Is there any difference between accessing ApplicationInfo through Activity versus Service components?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值