Android解析包名信息
16lz
2021-01-23
public ApplicationRet.Application getApkFileInfo(Context ctx, String apkPath) {File apkFile = new File(apkPath);if (!apkFile.exists() || !apkPath.toLowerCase().endsWith(".apk")) {return null;}ApplicationRet.Application appInfoData;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<Resources> resCt = Resources.class.getConstructor(typeArgs);valueArgs = new Object[3];valueArgs[0] = assetMag;valueArgs[1] = res.getDisplayMetrics();valueArgs[2] = res.getConfiguration();res = (Resources) resCt.newInstance(valueArgs);PackageManager pm = ctx.getPackageManager();// 读取apk文件的信息appInfoData = new ApplicationRet.Application();if (info != null) {if (info.icon != 0) {// 图片存在,则读取相关信息Drawable icon = res.getDrawable(info.icon);// 图标appInfoData.setLocalAppIcon(icon);}if (info.labelRes != 0) {String name = (String) res.getText(info.labelRes);// 名字appInfoData.setTitle(name);} else {String loadLabelName = info.loadLabel(pm).toString();if(loadLabelName == null || "".equals(loadLabelName)) {String apkName = apkFile.getName();appInfoData.setTitle(apkName.substring(0,apkName.lastIndexOf(".")));} else {appInfoData.setTitle(loadLabelName);}}String pkgName = info.packageName;// 包名appInfoData.setPkg(pkgName);} else {return null;}PackageInfo packageInfo = pm.getPackageArchiveInfo(apkPath,PackageManager.GET_ACTIVITIES);if (packageInfo != null) {appInfoData.setVersionName(packageInfo.versionName);appInfoData.setVersion(packageInfo.versionCode);// 版本码}appInfoData.setIsAppFlag(ActionController.manager_download_app_flag_downloaded);return appInfoData;} catch (Exception e) {e.printStackTrace();}return null;}
if (info.labelRes != 0) {String name = (String) res.getText(info.labelRes);// 名字appInfoData.setTitle(name);} else {String loadLabelName = info.loadLabel(pm).toString();if(loadLabelName == null || "".equals(loadLabelName)) {String apkName = apkFile.getName();appInfoData.setTitle(apkName.substring(0,apkName.lastIndexOf(".")));} else {appInfoData.setTitle(loadLabelName);}}注意红色标注部分,在取apk应用程序名的时候,采用 info.labelRes方法 有可能取不到,原因是开发者没有将应用程序的应用名写到String.xml中,而是直接写到了AndroidManifest.xml中的label标签中,如:android:label="包名", 这样用info.labelRes就获取不到,只能用info.loadLabel()方法。
更多相关文章
- android抓取各种log的方法
- ubuntu 不是 识别 android 设备 解决方法
- Android EditText中添加图标的简单方法
- android隐藏以及显示软键盘以及不自动弹出键盘的方法
- Ubuntu下Eclipse开发Android经常崩溃的解决方法
- Android 常用画图方法练习
- android TextView setEms 方法名字
- Android级联菜单的实现方法
- Android实现手机震动抖动效果的方法