相关类介绍
PackageManager
不属于系统级服务,放在这个类别里是因为它也是xManager
PackageManager:包管理者,可以获取单个应用的
PackageInfo
,可以获取多个应用的PackageInfo
集合。
PackageInfo:包含versionCode
+versionName
+ApplicationInfo
ApplicationInfo:包含应用的名称+图标+安装包路径+包名
PackageInfo
PackageManager pm = getPackageManager();
PackageInfo packageInfo = pm.getPackageInfo("com.android.mobilesafe", 0);//??
localVersionCode = packageInfo.versionCode;
localVersionName = packageInfo.versionName;
tv_version_name.setText(localVersionName);
获取已经安装的所有包
PackageManager pm = getPackageManager();
List<PackageInfo> packages = pm.getInstalledPackages(0);
ApplicationInfo
获取应用详情
for (PackageInfo packageInfo : packages) {
ApplicationInfo info = packageInfo.applicationInfo;
Drawable icon = info.loadIcon(pm);
String label = info.loadLabel(pm).toString();
String name = info.name;
String sourceDir = info.sourceDir;
String packageName = packageInfo.packageName;
}
loadIcon(pm):才是图标,loadLogo(pm)不是。
label:才是应用的名称.
name:值是null
sourceDir: apk的路径
packageName:包名
String name = info.name;//null
sourceDir=/data/app/so.ofo.labofo-1/base.apk
label=ofo共享单车
packageName=so.ofo.labofo
判断是不是系统应用?结果为true
就是系统应用。
(applicationIno.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
bean.isSystem = true;
}
判断应用安装再手机还是SD卡?结果为true
就是系统应用。
(info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE
启动APP
PackageManager pm = getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage(info.getPackageName());
if (launchIntent != null) {
startActivity(launchIntent);
}