[Android] teaches you how to get resolveinfo (non-reflective, manual parsing) for apps that are not installed

Source: Internet
Author: User

Principle: The principle is not much said, look under the Android source code will know. First get Assetmanager, then use Assetmanager Openxmlresourceparser method to get Xmlresourceparser, Start parsing the Androidmanifest.xml file in apk with Xmlresourceparser
Solution: Directly on the code bar, very simple.
Package Com.example.linetest;import Java.lang.reflect.method;import Java.util.hashmap;import java.util.Map;import Org.xmlpull.v1.xmlpullparser;import Android.annotation.suppresslint;import Android.content.intentfilter;import Android.content.res.assetmanager;import Android.content.res.xmlresourceparser;import Android.os.PatternMatcher; Import Android.text.textutils;public class Intentfilterparser {string Apkpath;public intentfilterparser (string Apkpath) {This.apkpath = Apkpath;} Public map<string, Intentfilter> Getintentfilter () {map<string, intentfilter> intentfilters = new HashMap <string, intentfilter> (); Assetmanager Assetmanager = null; Xmlresourceparser parser = null;try {Assetmanager = AssetManager.class.newInstance (); method = AssetManager.class.getDeclaredMethod ("Addassetpath", String.class); Method.invoke (Assetmanager, Apkpath);p Arser = Assetmanager.openxmlresourceparser (android_manifest_filename);p arsemanifest (parser, intentfilters);p arser.close (); AssetmanagEr.close ();p arser = Null;assetmanager = null;} catch (Exception e) {} finally {if (parser! = null) {parser.close ();p arser = null;} if (Assetmanager! = null) {assetmanager.close (); assetmanager = null;}} return intentfilters;} private void Parsemanifest (Xmlresourceparser parser, map<string, intentfilter> intentfilters) throws Exception { int outerdepth = parser.getdepth (); int Type;while ((type = Parser.next ())! = Xmlpullparser.end_document && (type!) = Xmlpullparser.end_tag | | Parser.getdepth () > Outerdepth)) {if (type = = Xmlpullparser.end_tag | | type = = xmlpullparser.text) {continue;} String tagName = Parser.getname (); if (tagName = = null) {continue;} if (Tagname.equals ("activity") | | tagname.equals ("receiver") | | tagname.equals ("service") {String activityname = Parser.getattributevalue (android_resources, "name"), if (activityname! = null) {parseactivity (Activityname, Intentfilters, parser);}}} private void Parseactivity (String activityname, map<string, intentfilter> inteNtfilters, Xmlresourceparser parser) throws Exception {int outerdepth = parser.getdepth (); int Type;while ((type = PARSER.N  EXT ()) = Xmlpullparser.end_document && (type! = Xmlpullparser.end_tag | | parser.getdepth () > Outerdepth)) {if (type = = Xmlpullparser.end_tag | | type = = xmlpullparser.text) {continue;} String tagName = Parser.getname (); if (tagName = = null) {continue;} if (Tagname.equals ("Intent-filter")) {Intentfilter mfilter = new Intentfilter (); Intentfilters.put (ActivityName, Mfilter);p Arseintentfilter (mfilter, parser);}} /** * Parse intent-filter field * * @param mfilter * @param attrs * @throws Exception * * @SuppressLint ("Newapi") private void Parse Intentfilter (Intentfilter mfilter, Xmlresourceparser attrs) throws Exception {int outerdepth = attrs.getdepth (); int type  ; while (type = Attrs.next ())! = Xmlpullparser.end_document && (type! = Xmlpullparser.end_tag | | attrs.getdepth () > outerdepth) {if (type = = Xmlpullparser.end_tag | | type = = xmlpullparser.text) {continue;}string nodeName = Attrs.getname (); if (nodeName = = null) {continue;} if (Nodename.equals ("action")) {String value = Attrs.getattributevalue (android_resources, "name"); Textutils.isempty (value)) {mfilter.addaction (value);}} else if (nodename.equals ("category")) {String value = Attrs.getattributevalue (android_resources, "name"); Textutils.isempty (value)) {mfilter.addcategory (value);}} else if (nodename.equals ("data")) {/** * Specify a URI authority host is handled, as per * intentfilter.adddataauthor ity (). [string] */string host = Attrs.getattributevalue (Android_resources, "host");/** * Specify a URI authority port that is Han Dled, as per * intentfilter.adddataauthority (). [string] */string port = attrs.getattributevalue (android_resources, "Port"); Textutils.isempty (host) &&! Textutils.isempty (port) {mfilter.adddataauthority (host, port);} /** * Specify a URI scheme that is handled, as per * Intentfilter.adddatascheme (). [string] */string scheme = Attrs.getattriButevalue (android_resources, "scheme"); Textutils.isempty (Scheme)) {mfilter.adddatascheme (scheme);} /** * Specify a URI path that must exactly match, as per * Intentfilter.adddatapath () with Pattern_literal. [string] */string Path = Attrs.getattributevalue (android_resources, "path"); Textutils.isempty (Path)) {Mfilter.adddatapath (path, patternmatcher.pattern_literal);} /** * Specify a URI path that must is a prefix to match, as per * Intentfilter.adddatapath () with Pattern_prefix. [string] */string Pathprefix = Attrs.getattributevalue (android_resources, "Pathprefix"); Textutils.isempty (Pathprefix)) {Mfilter.adddatapath (Pathprefix, patternmatcher.pattern_prefix);} /** * Specify a URI path that matches a simple pattern, as per * Intentfilter.adddatapath () with Pattern_simple_glob. [string] */string Pathpattern = Attrs.getattributevalue (android_resources, "Pathpattern"); Textutils.isempty (Pathpattern)) {Mfilter.adddatapath (Pathpattern, Patternmatcher.pattern_simple_glob);} /*** Specify a MIME type is handled, as per * Intentfilter.adddatatype (). [string] */string MimeType = Attrs.getattributevalue (android_resources, "MimeType"); Textutils.isempty (MimeType)) {Mfilter.adddatatype (mimeType);}  if (Android.os.Build.VERSION.SDK_INT >=) {/** * Specify a URI scheme specific part that must exactly * match, as per Intentfilter.adddataschemespecificpart () * with Pattern_literal. [string] */string SSP = attrs.getattributevalue (android_resources, "SSP"); Mfilter.adddataschemespecificpart (SSP, patternmatcher.pattern_literal);/** * Specify a URI scheme specific part, must is a prefix * to match, as per Intentfi Lter.adddataschemespecificpart () * with Pattern_prefix. [string] */string Sspprefix = Attrs.getattributevalue (android_resources, "Sspprefix"); Mfilter.adddataschemespecificpart (Sspprefix, patternmatcher.pattern_prefix);/** * Specify a URI scheme specific part That is matches a simple * pattern, as per Intentfilter.adddataschemespecificpart () * with PATTern_simple_glob. [string] */string Ssppattern = Attrs.getattributevalue (android_resources, "Ssppattern"); Mfilter.adddataschemespecificpart (Ssppattern, Patternmatcher.pattern_simple_glob);}}} private static final String android_resources = "http://schemas.android.com/apk/res/android";p rivate static final String android_manifest_filename = "Androidmanifest.xml";}


[Android] teaches you how to get resolveinfo (non-reflective, manual parsing) for apps that are not installed

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.