PackageInfo类包含AndroidManifest.xml文件的信息。
一些常用的属性如下:
获得PackageInfo
//获取指定包名的packageInfo,并且包含所有的内容提供者
val pack = context.packageManager.getPackageInfo(
context.packageName,
PackageManager.GET_PROVIDERS
)
//获得手机中安装了的应用的packageInfo,并且包含所有的内容提供者(PackageManager.GET_PROVIDERS)
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
想获得什么信息,就填什么flag ,如果想要获得所有的activity
val pack = context.packageManager.getPackageInfo(
context.packageName,
PackageManager.GET_ACTIVITIES
)
示例:
1.不添加任何flag
fun getRealPath(context: Context): String? {
try {
val pack = context.packageManager.getPackageInfo(
context.packageName,
0
)
获得的PackageInfo的一些信息
pack = {PackageInfo@9975} "PackageInfo{1956f2d com.example.mytest2}"
activities = null //获取的activity为空
applicationInfo = {ApplicationInfo@9992} "ApplicationInfo{e4e3c62 com.example.mytest2}"
baseRevisionCode = 0
compileSdkVersion = 32
compileSdkVersionCodename = "12"
configPreferences = null
coreApp = false
featureGroups = null
firstInstallTime = 1686015434025
gids = null
installLocation = -1
instrumentation = null
isApex = false
isStub = false
lastUpdateTime = 1686214196863
mOverlayIsStatic = false
overlayCategory = null
overlayPriority = 0
overlayTarget = null
packageName = "com.example.mytest2"
permissions = null
providers = null
receivers = null
reqFeatures = null
requestedPermissions = null
requestedPermissionsFlags = null
requiredAccountType = null
requiredForAllUsers = false
restrictedAccountType = null
services = null
sharedUserId = null
sharedUserLabel = 0
signatures = null
signingInfo = null
splitNames = null
splitRevisionCodes = null
targetOverlayableName = null
versionCode = 0
versionCodeMajor = 0
versionName = "1.0"
shadow$_klass_ = {Class@1937} "class android.content.pm.PackageInfo"
shadow$_monitor_ = -2120913107
2.添加flags,添加了获得activity的flags
val pack = context.packageManager.getPackageInfo(
context.packageName,
PackageManager.GET_ACTIVITIES
)
上面不添加flags的获取不到activity,这个可以获取的到
pack = {PackageInfo@9982} "PackageInfo{cd01809 com.example.mytest2}"
activities = {ActivityInfo[6]@9999} //获取到了activity
applicationInfo = {ApplicationInfo@10000} "ApplicationInfo{be32a0e com.example.mytest2}"
baseRevisionCode = 0
compileSdkVersion = 32
compileSdkVersionCodename = "12"
configPreferences = null
coreApp = false
featureGroups = null
firstInstallTime = 1686015434025
gids = null
installLocation = -1
instrumentation = null
isApex = false
isStub = false
lastUpdateTime = 1686214391973
mOverlayIsStatic = false
overlayCategory = null
overlayPriority = 0
overlayTarget = null
packageName = "com.example.mytest2"
permissions = null
providers = null
receivers = null
reqFeatures = null
requestedPermissions = null
requestedPermissionsFlags = null
requiredAccountType = null
requiredForAllUsers = false
restrictedAccountType = null
services = null
sharedUserId = null
sharedUserLabel = 0
signatures = null
signingInfo = null
splitNames = null
splitRevisionCodes = null
targetOverlayableName = null
versionCode = 0
versionCodeMajor = 0
versionName = "1.0"
shadow$_klass_ = {Class@1937} "class android.content.pm.PackageInfo"
shadow$_monitor_ = -1932519415
获取APP信息方式:
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
// APP 包名
String packageName = packageInfo.packageName;
// APP icon
Drawable icon = packageManager.getApplicationIcon(applicationInfo);
// APP 名称
String appName = packageManager.getApplicationLabel(applicationInfo).toString();
// APP 权限
String[] permissions = packageManager.getPackageInfo(packageName,PackageManager.GET_PERMISSIONS).requestedPermissions;
ApplicationInfo类
ApplicationInfo是android.content.pm包下的一个实体类,用于封装应用的信息,flags是其中的一个成员变量public int flags = 0;用于保存应用的标志信息。
ApplicationInfo 可以得到一个应用基本信息。这些信息是从AndroidManifest.xml的< application >标签获取的
ApplicationInfo对象里保存的信息都是<application>标签里的属性值
ApplicationInfo与ResolveInfo比较:前者能够得到Icon、Label、meta-data、description。后者只能得到Icon、Label