[Android] teaches you how to get resolveinfo for apps that aren't installed

Source: Internet
Author: User

Principle: See the next Android source code, understand the resolution process, just their own words will be transplanted Android source code, someone resolved successfully, but I feel too troublesome. About the Android parsing process.
    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, Then through the packageparser.generatepackageinfo back to PackageInfo, but packageinfo in the intent-filter to remove, do not know why, but since know this piece of code so good to run. That is to get the packageparser.package through reflection and then encapsulate it into intentfilter. Solution: Directly on the code. Because it's all just a reflection and rewriting the code once.
  try {//Get parse Class Packageparser and instantiate class Packageparserclass = Class.forName ("Android.content.pm.PackageParser");   Object Packageparser = Packageparserclass.getconstructor (String.class). newinstance (Dexpath);   Build parameters Displaymetrics metrics = new Displaymetrics ();   Metrics.settodefaults ();   File SourceFile = new file (Dexpath); Call Packageparser's Parsepackage 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 reflection results from 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.G      ET (0). getaction (0);      String categorystring = intents.get (0). getcategory (0);     System.out.println (actionstring + "" + categorystring); }}}} catch (Exception e) {}
Dexpath is the path to the apk you want to parse. There are action and category in Intentfilter. This realizes to get an activity of the resolveinfo, as for why to type wandering, then the specific look at Android source code bar.

[Android] teaches you how to get resolveinfo for apps that aren't installed

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.