Android obtains information about installed and not installed apk.
PackageManager is the most important class for obtaining apk resources. We can use this class to get all kinds of things we want. First, we can get the basic information of installed apk, including label, and icons:
PackageManager pm = getPackageManager (); List
ApkInfos = pm. getInstalledPackages (0); ArrayList
Infos_name = new ArrayList
(); // Used to store labelArrayList
Infos_icon = newArrayList
(); // Used to store the apk startup icon resource String name = ""; Drawable icon; PackageInfo apk; for (int I = 0; I <apkInfos. size ();) {apk = apkInfos. get (I); I ++; name = (String) pm. getApplicationLabel (apk. applicationInfo); icon = pm. getApplicationIcon (apk. applicationInfo); infos_icon.add (icon); infos_name.add (name );}
Next, we use PackageManager to obtain the activity, service, and so on in the installed apk.
List
PackagesInfo = pm. getInstalledPackages (0); for (PackageInfo packageInfo: packagesInfo) {Log. d ("TAG", "packageInfo name is:" + packageInfo. packageName);} PackageManager pm = getPackageManager (); try {// get com. example. activity, service, and broadcastreceiverPackageInfo packageInfo = pm in the pertest package. getPackageInfo ("com. example. pertest ", PackageManager. GET_PERMISSIONS | PackageManager. GET_SERVICES | PackageManager. GET _ RECEIVERS); ActivityInfo [] activities = packageInfo. activities; if (activities! = Null) {for (ActivityInfo activityInfo: activities) {Log. d ("TAG", "the activity" + activityInfo. toString () + "=" + packageInfo. versionCode + "=" + packageInfo. versionName) ;}} else {Log. d ("TAG", "the activity is null");} ServiceInfo [] serviceInfos = packageInfo. services; for (ServiceInfo serviceInfo: serviceInfos) {Log. d ("TAG", "service name is:" + serviceInfo. name);} activities = packageInfo. receivers; for (ActivityInfo activity: activities) {Log. d ("TAG", "handler er is:" + activity. name) ;}} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();}
The above shows how to obtain the basic information of installed apk files. Next, let's take a look at how to obtain the basic information of uninstalled apk files. This is useful for android dynamic loading. Let's look at it:
Get the icon of apk not installed:
public static Drawable getAppIcon(Context context, String apkFilepath) { PackageManager pm = context.getPackageManager(); PackageInfo pkgInfo = getPackageInfo(context, apkFilepath); if (pkgInfo == null) { return null; } ApplicationInfo appInfo = pkgInfo.applicationInfo; if (Build.VERSION.SDK_INT >= 8) { appInfo.sourceDir = apkFilepath; appInfo.publicSourceDir = apkFilepath; } return pm.getApplicationIcon(appInfo); }
Get the name of the apk not installed:
public static CharSequence getAppLabel(Context context, String apkFilepath) { PackageManager pm = context.getPackageManager(); PackageInfo pkgInfo = getPackageInfo(context, apkFilepath); if (pkgInfo == null) { return null; } ApplicationInfo appInfo = pkgInfo.applicationInfo; if (Build.VERSION.SDK_INT >= 8) { appInfo.sourceDir = apkFilepath; appInfo.publicSourceDir = apkFilepath; } return pm.getApplicationLabel(appInfo); }
If we still don't think it is enough, we can get the PackageInfo object. Through this object, we can get the activity, service, broadcastreceiver and so on which the apk is not installed, the method is the same as "Get activity, and service in installed apk". The Code is as follows:
// Obtain the PackageInfo object, which contains the activity and servicepublic static PackageInfo getPackageInfo (Context context, String apkFilepath) of the apk {PackageManager pm = context. getPackageManager (); PackageInfo pkgInfo = null; try {pkgInfo = pm. getPackageArchiveInfo (apkFilepath, PackageManager. GET_ACTIVITIES | PackageManager. GET_SERVICES);} catch (Exception e) {// shocould be something wrong with parse e. printStackTrace ();} return pkgInfo ;}
During android dynamic loading, DexClassLoader is used to obtain the DexClassLoader object of the uninstalled program:
String mNativeLibDir = mContext.getDir("pluginlib", Context.MODE_PRIVATE).getAbsolutePath(); private DexClassLoader createDexClassLoader(String dexPath) { File dexOutputDir = mContext.getDir("dex", Context.MODE_PRIVATE); dexOutputPath = dexOutputDir.getAbsolutePath(); DexClassLoader loader = new DexClassLoader(dexPath, dexOutputPath, mNativeLibDir, mContext.getClassLoader()); return loader; }
The first parameter dexPath is the storage path of our apk.
The second parameter dexOutPath is the storage path of the dex file corresponding to the apk file. It cannot be null.
The third parameter is the list of C/C ++ libraries used in the target class.File. pathSeparator
; Can benull。
The fourth parameter is the parent loader of the Class Loader. It is generally used as the loader of the current execution class.
After obtaining the DexClassLoader object, we can dynamically load the methods in the apk. For detailed methods, see another article: android dynamic loading.
For loading resources in an uninstalled apk, because different context is used, do the following first:
private AssetManager createAssetManager(String dexPath) { try { AssetManager assetManager = AssetManager.class.newInstance(); Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class); addAssetPath.invoke(assetManager, dexPath); return assetManager; } catch (Exception e) { e.printStackTrace(); return null; } }private Resources createResources(AssetManager assetManager) { Resources superRes = mContext.getResources(); Resources resources = new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration()); return resources;}
In this case, assetManager and resources can be used in the same way.