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;
}
另外這個方式也可以實現。
總結
以上是生活随笔為你收集整理的Android如何打开未安装的apk,android获取未安装的APK文件的信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Java建立一个公交管理系统,监督管
- 下一篇: 【Python爬虫】从零开始爬取Sci-