众所周知,通过PackageManager可以获取手机端已安装的apk文件的信息,具体代码如下
view plaincopy to clipboardprint?
PackageManager packageManager = this.getPackageManager();
List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);
PackageManager packageManager = this.getPackageManager();
List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);
通过以上方法,可以得到手机中安装的所有应用程序,既包括了手动安装的apk包的信息,也包括了系统预装的应用软件的信息,要区分这两类软件可使用以下方法:
a.从packageInfoList获取的packageInfo,再通过packageInfo.applicationInfo获取applicationInfo。
b.判断(applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)的值,该值大于0时,表示获取的应用为系统预装的应用,反之则为手动安装的应用。
大家可以看下代码,说明已经写在注释中。
获取应用的代码:
view plaincopy to clipboardprint?
/**
* 查询手机内非系统应用
* @param context
* @return
*/
public static List<PackageInfo> getAllApps(Context context) {
List<PackageInfo> apps = new ArrayList<PackageInfo>();
PackageManager pManager = context.getPackageManager();
//获取手机内所有应用
List<PackageInfo> paklist = pManager.getInstalledPackages(0);
for (int i = 0; i < paklist.size(); i++) {
PackageInfo pak = (PackageInfo) paklist.get(i);
//判断是否为非系统预装的应用程序
if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
// customs applications
apps.add(pak);
}
}
return apps;
}
/**
* 查询手机内非系统应用
* @param context
* @return
*/
public static List<PackageInfo> getAllApps(Context context) {
List<PackageInfo> apps = new ArrayList<PackageInfo>();
PackageManager pManager = context.getPackageManager();
//获取手机内所有应用
List<PackageInfo> paklist = pManager.getInstalledPackages(0);
for (int i = 0; i < paklist.size(); i++) {
PackageInfo pak = (PackageInfo) paklist.get(i);
//判断是否为非系统预装的应用程序
if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
// customs applications
apps.add(pak);
}
}
return apps;
}
获取图片、应用名、包名:
view plaincopy to clipboardprint?
PackageManager pManager = MessageSendActivity.this.getPackageManager();
List<PackageInfo> appList = Utils.getAllApps(MessageSendActivity.this);
for(int i=0;i<appList.size();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);
}
PackageManager pManager = MessageSendActivity.this.getPackageManager();
List<PackageInfo> appList = Utils.getAllApps(MessageSendActivity.this);
for(int i=0;i<appList.size();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 类是我本地自定义的,各位可以忽略!
另为,有人可能在找 分享应用列表,在此一并说下吧。
获取支持分享的应用的代码:
view plaincopy to clipboardprint?
/**
* 查询手机内所有支持分享的应用
* @param context
* @return
*/
public static List<ResolveInfo> getShareApps(Context context){
List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
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;
}
/**
* 查询手机内所有支持分享的应用
* @param context
* @return
*/
public static List<ResolveInfo> getShareApps(Context context){
List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
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。因此获取图片、应用名、包名的方法不一样,如下:
view plaincopy to clipboardprint?
PackageManager pManager = MessageSendActivity.this.getPackageManager();
/*********************查询手机内所有支持分享的应用*********************/
List<ResolveInfo> resolveList = Utils.getShareApps(MessageSendActivity.this);
for(int i=0;i<resolveList.size();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);
}
PackageManager pManager = MessageSendActivity.this.getPackageManager();
/*********************查询手机内所有支持分享的应用*********************/
List<ResolveInfo> resolveList = Utils.getShareApps(MessageSendActivity.this);
for(int i=0;i<resolveList.size();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()