Android如何打开未安装的apk,android获取未安装的APK文件的信息

下面从一个未安装的android的apk文件获取apk信息

/**

* 获取未安装的apk信息

*

* @param ctx Context

* @param apkPath apk路径,可以放在SD卡

* @return

*/

public static ApkFileInfo getApkFileInfo(Context ctx, String apkPath)

{

System.out.println(apkPath);

File apkFile = new File(apkPath);

if (!apkFile.exists() || !apkPath.toLowerCase().endsWith(".apk"))

{

System.out.println("file path is not correct");

return null;

}

ApkFileInfo apkFileInfo;

String PATH_PackageParser = "android.content.pm.PackageParser";

String PATH_AssetManager = "android.content.res.AssetManager";

try

{

//反射得到pkgParserCls对象并实例化,有参数

Class> pkgParserCls = Class.forName(PATH_PackageParser);

Class>[] typeArgs = {String.class};

Constructor> pkgParserCt = pkgParserCls.getConstructor(typeArgs);

Object[] valueArgs = {apkPath};

Object pkgParser = pkgParserCt.newInstance(valueArgs);

//从pkgParserCls类得到parsePackage方法

DisplayMetrics metrics = new DisplayMetrics();

metrics.setToDefaults();//这个是与显示有关的, 这边使用默认

typeArgs = new Class>[]{File.class,String.class,

DisplayMetrics.class,int.class};

Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage", typeArgs);

valueArgs=new Object[]{new File(apkPath),apkPath,metrics,0};

//执行pkgParser_parsePackageMtd方法并返回

Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs);

//从返回的对象得到名为"applicationInfo"的字段对象

if (pkgParserPkg==null)

{

return null;

}

Field appInfoFld = pkgParserPkg.getClass().getDeclaredField("applicationInfo");

//从对象"pkgParserPkg"得到字段"appInfoFld"的值

if (appInfoFld.get(pkgParserPkg)==null)

{

return null;

}

ApplicationInfo info = (ApplicationInfo) appInfoFld.get(pkgParserPkg);

//反射得到assetMagCls对象并实例化,无参

Class> assetMagCls = Class.forName(PATH_AssetManager);

Object assetMag = assetMagCls.newInstance();

//从assetMagCls类得到addAssetPath方法

typeArgs = new Class[1];

typeArgs[0] = String.class;

Method assetMag_addAssetPathMtd = assetMagCls.getDeclaredMethod("addAssetPath", typeArgs);

valueArgs = new Object[1];

valueArgs[0] = apkPath;

//执行assetMag_addAssetPathMtd方法

assetMag_addAssetPathMtd.invoke(assetMag, valueArgs);

//得到Resources对象并实例化,有参数

Resources res = ctx.getResources();

typeArgs = new Class[3];

typeArgs[0] = assetMag.getClass();

typeArgs[1] = res.getDisplayMetrics().getClass();

typeArgs[2] = res.getConfiguration().getClass();

Constructor resCt = Resources.class.getConstructor(typeArgs);

valueArgs = new Object[3];

valueArgs[0] = assetMag;

valueArgs[1] = res.getDisplayMetrics();

valueArgs[2] = res.getConfiguration();

//这个是重点

//得到Resource对象后可以有很多用处

res = (Resources) resCt.newInstance(valueArgs);

// 读取apk文件的信息

apkFileInfo = new ApkFileInfo();

if (info!=null)

{

apkFileInfo.setPath(apkPath);

if (info.icon != 0)

{

// 图片存在,则读取相关信息

Drawable icon = res.getDrawable(info.icon);// 图标

apkFileInfo.setDrawable(icon);

}

if (info.labelRes != 0)

{

String neme = (String) res.getText(info.labelRes);// 名字

apkFileInfo.setapkFileName(neme);

}else

{

String apkName=apkFile.getName();

apkFileInfo.setapkFileName(apkName.substring(0,apkName.lastIndexOf(".")));

}

String pkgName = info.packageName;// 包名

apkFileInfo.setPackageName(pkgName);

}

else

{

return null;

}

PackageManager pm = ctx.getPackageManager();

PackageInfo packageInfo = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);

if (packageInfo != null)

{

apkFileInfo.setAppVersion(packageInfo.versionName);//版本号

}

return apkFileInfo;

} catch (Exception e)

{

e.printStackTrace();

}

return null;

}

另外这个方式也可以实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值