众所周知,通过PackageManager可以获取手机端已安装的apk文件的信息,具体代码如下:
PackageManager packageManager =
this.getPackageManager();
List packageInfoList = packageManager.getInstalledPackages(
0);
通过以上方法,可以得到手机中安装的所有应用程序,既包括了手动安装的apk包的信息,也包括了系统预装的应用软件的信息,要区分这两类软件可使用以下方法:
a.从packageInfoList获取的packageInfo,再通过packageInfo.applicationInfo获取applicationInfo。
b.判断(applicationInfo.flags& ApplicationInfo.FLAG_SYSTEM)的值,该值大于0时,表示获取的应用为系统预装的应用,反之则为手动安装的应用。
大家可以看下代码,说明已经写在注释中。
获取应用的代码:
public
static List getAllApps(Context context) {
List apps =
new ArrayList();
PackageManager pManager = context.getPackageManager();
//
获取手机内所有应用
List paklist = pManager.getInstalledPackages(
0);
for (
int i =
0; i
PackageInfo pak = (PackageInfo) paklist.
get(i);
//
判断是否为非系统预装的应用程序
if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <=
0) {
//
customs applications
apps.add(pak);
}
}
return apps;
}
获取图片、应用名、包名:
PackageManager pManager = MessageSendActivity.
this.getPackageManager();
List appList = Utils.getAllApps(MessageSendActivity.
this);
for(
int i=
0;i
PackageInfo pinfo = appList.
get(i);
ShareItemInfo shareItem =
new ShareItemInfo();
//
set Icon
shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo));
//
set Application Name
shareItem.setLabel(pManager.getApplicationLabel(pinfo.applicationInfo).toString());
//
set Package Name
shareItem.setPackageName(pinfo.applicationInfo.packageName);
}
其中 ShareItemInfo 类是我本地自定义的,各位可以忽略!
另为,有人可能在找 分享应用列表,在此一并说下吧。
获取支持分享的应用的代码:
public
static List getShareApps(Context context){
List mApps =
new ArrayList();
Intent intent=
new Intent(Intent.ACTION_SEND,
null);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType(
"
text/plain
");
PackageManager pManager = context.getPackageManager();
mApps = pManager.queryIntentActivities(intent,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
return mApps;
}
由于该方法,返回的并不是 PackageInfo 对象。而是 ResolveInfo。因此获取图片、应用名、包名的方法不一样,如下:
PackageManager pManager = MessageSendActivity.
this.getPackageManager();
List resolveList = Utils.getShareApps(MessageSendActivity.
this);
for(
int i=
0;i
ResolveInfo resolve = resolveList.
get(i);
ShareItemInfo shareItem =
new ShareItemInfo();
//
set Icon
shareItem.setIcon(resolve.loadIcon(pManager));
//
set Application Name
shareItem.setLabel(resolve.loadLabel(pManager).toString());
//
set Package Name
shareItem.setPackageName(resolve.activityInfo.packageName);
}
总结:
通过 PackageInfo 获取具体信息方法:
包名获取方法:packageInfo.packageName
icon获取获取方法:packageManager.getApplicationIcon(applicationInfo)
应用名称获取方法:packageManager.getApplicationLabel(applicationInfo)
使用权限获取方法:packageManager.getPackageInfo(packageName,PackageManager.GET_PERMISSIONS)
.requestedPermissions
通过 ResolveInfo 获取具体信息方法:
包名获取方法:resolve.activityInfo.packageName
icon获取获取方法:resolve.loadIcon(packageManager)
应用名称获取方法:resolve.loadLabel(packageManager).toString()