文件管理器--取得apk安装包的信息

//通过反射
//取得apk安装包的信息
public static ApplicationInfo getApplicationInfo(Context context,String archiveFilePath) {
		String PACKAGE_PARSER_CLASS_NAME = "android.content.pm.PackageParser";

		ApplicationInfo applicationInfo = null;
		
		try {
			//反射取得PackageParser类的class
			Class<?> packageParserClass = Class.forName(PACKAGE_PARSER_CLASS_NAME);

			Class<?>[] parameterTypes = new Class[1];
			parameterTypes[0] = String.class;
			//取得一个构造方法
			Constructor<?> packageParserConstructor = packageParserClass.getConstructor(parameterTypes);
			
			Object[] args = new Object[1];
			//PackageParser类的构造方法的参数 
			args[0] = archiveFilePath;
			//取得一个packageparser类的实例
			Object packageParser = packageParserConstructor.newInstance(args);

			Resources pRes = context.getResources();

			parameterTypes = new Class[4];
			parameterTypes[0] = File.class;
			parameterTypes[1] = String.class;
			parameterTypes[2] = pRes.getDisplayMetrics().getClass();
			parameterTypes[3] = Integer.TYPE;
			//得到parsePackage方法,有参数 public Package parsePackage(File sourceFile, String destCodePath,
            //DisplayMetrics metrics, int flags) 
			Method parsePackage = packageParserClass.getDeclaredMethod("parsePackage", parameterTypes);

			args = new Object[4];
			args[0] = new File(archiveFilePath);
			args[1] = archiveFilePath;
			args[2] = pRes.getDisplayMetrics();
			args[3] = 0;
			Object packageInfo = parsePackage.invoke(packageParser, args);
			//取得成员字段
			Field applicationInfoField = packageInfo.getClass().getDeclaredField("applicationInfo");
			applicationInfo = (ApplicationInfo) applicationInfoField.get(packageInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return applicationInfo;
	}
//取得apk文件的资源
public static Resources getResource(Context context, String archiveFilePath) {
		String ASSET_MANAGER_CLASS_NAME = "android.content.res.AssetManager";
		Resources resources = null;

		try {
			Class<?> assetManagerClass = Class.forName(ASSET_MANAGER_CLASS_NAME);

			Constructor<?> assetManagerConstructor = assetManagerClass.getConstructor((Class[]) null);
			Object assetManager = assetManagerConstructor.newInstance((Object[]) null);

			Class<?>[] parameterTypes = new Class[1];
			parameterTypes[0] = String.class;
			Method addAssetPath = assetManagerClass.getDeclaredMethod("addAssetPath", parameterTypes);

			Object[] args = new Object[1];
			args[0] = archiveFilePath;
			addAssetPath.invoke(assetManager, args);

			Resources pRes = context.getResources();

			parameterTypes = new Class[3];
			parameterTypes[0] = assetManager.getClass();
			parameterTypes[1] = pRes.getDisplayMetrics().getClass();
			parameterTypes[2] = pRes.getConfiguration().getClass();
			Constructor<?> resourcesConstructor = Resources.class.getConstructor(parameterTypes);

			args = new Object[3];
			args[0] = assetManager;
			args[1] = pRes.getDisplayMetrics();
			args[2] = pRes.getConfiguration();
			resources = (Resources) resourcesConstructor.newInstance(args);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return resources;
	}

/**
	 * 得到代表apk文件的图标
	 * @param context
	 * @param file
	 * @return
	 */
	public static Drawable getApkIcon(Context context, File file) {
		Drawable icon = null;
		PackageManager pm = context.getPackageManager();
		ApplicationInfo applicationInfo = getApplicationInfo(context, file.getPath());
		Resources resources = getResource(context, file.getPath());
		
		if (applicationInfo == null || resources == null) {
			icon = pm.getDefaultActivityIcon();
		} else{
			if (applicationInfo.icon != 0) {
				try {
					icon = resources.getDrawable(applicationInfo.icon);
				} catch (Resources.NotFoundException e) {
				}
			}
			if (icon == null) {
				icon = pm.getDefaultActivityIcon();
			}
		}

		int iconSize = context.getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
		icon.setBounds(0, 0, iconSize, iconSize);
		return icon;
	}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值