Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android解析包名信息

Android解析包名信息

編輯:關於Android編程

[java]
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); 
                } 
                <span style="background-color: rgb(255, 102, 102);">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); 
                    } 
                }</span> 
                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; 
    } 
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);
    }
    <span style="background-color: rgb(255, 102, 102);">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);
     }
    }</span>
    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;
 }

 

 

 

[java] view plaincopyprint?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中, www.2cto.com
而是直接寫到了AndroidManifest.xml中的label標簽中,如:android:label="包名" 
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="包名"

 作者;xyylchq
 
 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved