概述
public List<AppInfo> getAppInfo() { // 获得PackageManager对象 mPm = this.getPackageManager(); List<PackageInfo> packageInfoList = mPm.getInstalledPackages(0); // 根据name排序 Collections.sort(packageInfoList, new Comparator<PackageInfo>() { @Override public int compare(PackageInfo p1, PackageInfo p2) { String name1 = (String) mPm.getApplicationLabel(p1.applicationInfo); String name2 = (String) mPm.getApplicationLabel(p2.applicationInfo); return name1.compareTo(name2); } }); mInfoList = new ArrayList<>(); for(PackageInfo info :packageInfoList){ AppInfo appInfo = new AppInfo(); //应用名称 String appName = (String) mPm.getApplicationLabel(info.applicationInfo); //图标 Drawable icon = info.applicationInfo.loadIcon(mPm); //包名 String packageName = info.applicationInfo.packageName; //路径 String path = info.applicationInfo.sourceDir; //大小 File file=new File(path); String size = formatFileSize(file.length()); //版本 String versionName = info.versionName; //第一次安装时间 long firstInstallTime = info.firstInstallTime; SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d1 =new Date(firstInstallTime); String firstTime=format.format(d1); appInfo.setPkgName(packageName); appInfo.setAppIcon(icon); appInfo.setVersionName(versionName); appInfo.setAppName(appName); appInfo.setPkgPath(path); appInfo.setCodeSize(size); appInfo.setAppDate(firstTime); mInfoList.add(appInfo); } return mInfoList; } /** * 格式化 * @param length * @return */ public static String formatFileSize(long length) { String result = null; int sub_string = 0; // 如果文件长度大于1GB if (length >= 1073741824) { sub_string = String.valueOf((float) length / 1073741824).indexOf( "."); result = ((float) length / 1073741824 + "000").substring(0, sub_string + 3) + "GB"; } else if (length >= 1048576) { // 如果文件长度大于1MB且小于1GB,substring(int beginIndex, int endIndex) sub_string = String.valueOf((float) length / 1048576).indexOf("."); result = ((float) length / 1048576 + "000").substring(0, sub_string + 3) + "MB"; } else if (length >= 1024) { // 如果文件长度大于1KB且小于1MB sub_string = String.valueOf((float) length / 1024).indexOf("."); result = ((float) length / 1024 + "000").substring(0, sub_string + 3) + "KB"; } else if (length < 1024) result = Long.toString(length) + "B"; return result; } /** * 根据包名启动APP * @param packagename 包名 */ private void doStartApplicationWithPackageName(String packagename) { // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null); resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER); resolveIntent.setPackage(packagename); // 通过getPackageManager()的queryIntentActivities方法遍历 List<ResolveInfo> resolveinfoList = getPackageManager() .queryIntentActivities(resolveIntent, 0); if(resolveinfoList.size() == 0){ return; } ResolveInfo resolveinfo = resolveinfoList.iterator().next(); if (resolveinfo != null) { String packageName = resolveinfo.activityInfo.packageName; //App启动的Activity String className = resolveinfo.activityInfo.name; // LAUNCHER Intent Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); // 设置ComponentName参数1:packagename参数2:启动的Activity ComponentName cn = new ComponentName(packageName, className); intent.setComponent(cn); startActivity(intent); } }
最后
以上就是超级香氛为你收集整理的获取手机所有安装APP的信息的全部内容,希望文章能够帮你解决获取手机所有安装APP的信息所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复