获取手机中安装的所有应用程序
PackageManager packageManager = this.getPackageManager(); List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);
通过以上方法,可以得到手机中安装的所有应用程序,既包括了手动安装的apk包的信息,也包括了系统预装的应用软件的信息,要区分这两类软件可使用以下方法:
a.从packageInfoList获取的packageInfo,再通过packageInfo.applicationInfo获取applicationInfo。
b.判断(applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)的值,该值大于0时,表示获取的应用为系统预装的应用,反之则为手动安装的应用。
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; }
获取图片、应用名、包名:
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); }
/** * 查询手机内所有支持分享的应用 * @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; }
本文地址:http://www.yidin.net/?p=9493
更多资料:http://www.yidin.net/discuz
更多相关的资料请到我的博客:www.yidin.net 留言
欢迎各位同学加入 android 技术二群 222392467