[Android] teaches you how to obtain ResolveInfo and androidresolveinfo for apps Not Installed

Source: Internet
Author: User

[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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.