[Android] teaches you how to obtain ResolveInfo and androidresolveinfo for apps Not Installed
Principle: I checked the android source code and understood the parsing process. I only needed to transplant the android source code. Someone successfully Parsed the code, but I thought it was too troublesome. Let's talk about the parsing process of Android.
public PackageInfo getPackageArchiveInfo(String archiveFilePath, int flags) { PackageParser packageParser = new PackageParser(archiveFilePath); DisplayMetrics metrics = new DisplayMetrics(); metrics.setToDefaults(); final File sourceFile = new File(archiveFilePath); PackageParser.Package pkg = packageParser.parsePackage( sourceFile, archiveFilePath, metrics, 0); if (pkg == null) { return null; } if ((flags & GET_SIGNATURES) != 0) { packageParser.collectCertificates(pkg, 0); } return PackageParser.generatePackageInfo(pkg, null, flags, 0, 0); }
Here is the source code, first through PackageParser parsing, and then through PackageParser. generatePackageInfo returns PackageInfo, but the intent-filter is removed from PackageInfo. I don't know why, but since I know this code, I should do it well. That is, PackageParser. Package is obtained through reflection, and then encapsulated into intentfilter. Solution: run the Code directly. Because the code is completely rewritten through reflection.
Try {// obtain the parsing Class PackageParser and instantiate Class packageParserClass = Class. forName ("android. content. pm. packageParser "); Object packageParser = packageParserClass. getConstructor (String. class ). newInstance (dexPath); // The construction parameter DisplayMetrics metrics = new DisplayMetrics (); metrics. setToDefaults (); File sourceFile = new File (dexPath); // call parsePackage of PackageParser to parse data Method = packageParserClass. getDeclaredMethod ("parsePackage", File. class, String. class, DisplayMetrics. class, int. class); method. setAccessible (true); Object Package = method. invoke (packageParser, sourceFile, dexPath, metrics, 0); // The result is Field activities = Package. getClass (). getDeclaredField ("activities"); activities. setAccessible (true); ArrayList filters = (ArrayList) activities. get (Package); for (int I = 0; I <filters. size (); I ++) {Object activity = filters. get (I); Field intentsField = activity. getClass (). getField ("intents"); intentsField. setAccessible (true); ArrayList <IntentFilter> intents = (ArrayList <IntentFilter>) intentsField. get (activity); for (int j = 0; j <intents. size (); j ++) {if (intents. iterator (). hasNext () {String actionString = intents. get (0 ). getAction (0); String categoryString = intents. get (0 ). getCategory (0); System. out. println (actionString + "" + categoryString) ;}}} catch (Exception e ){}
DexPath is the path of the APK to be parsed. Intentfilter contains action and category. In this way, we can get the ResolveInfo of an activity. For the reason why we need to transfer the type, please refer to the android source code for details.