不仅要给Intent设置Action, Type,还要设置Data,因为IntentFilter会根据Data得到Schema,而queryIntentActivities最后在match时,如果Activity的Intent-filter里配置了Schema,系统会要求查询的Intent里一定要有匹配的Schema才能解析出来。个人经验看,一般的Intent-filter里要么没有Schema,要么多是用"file"。
Intent resolveIntent = new Intent(Intent.ACTION_VIEW); resolveIntent.setDataAndType(Uri.parse("file://"), MIMEType); List<ResolveInfo> pkgAppsList = mContext.getPackageManager().queryIntentActivities(resolveIntent, PackageManager.MATCH_DEFAULT_ONLY| PackageManager.GET_RESOLVED_FILTER);
IntentFilter.java
/** * Match this filter against an Intent's data (type, scheme and path). If * the filter does not specify any types and does not specify any * schemes/paths, the match will only succeed if the intent does not * also specify a type or data. If the filter does not specify any schemes, * it will implicitly match intents with no scheme, or the schemes "content:" * or "file:" (basically performing a MIME-type only match). If the filter * does not specify any MIME types, the Intent also must not specify a MIME * type. * * <p>Be aware that to match against an authority, you must also specify a base * scheme the authority is in. To match against a data path, both a scheme * and authority must be specified. If the filter does not specify any * types or schemes that it matches against, it is considered to be empty * (any authority or data path given is ignored, as if it were empty as * well). * * <p><em>Note: MIME type, Uri scheme, and host name matching in the * Android framework is case-sensitive, unlike the formal RFC definitions. * As a result, you should always write these elements with lower case letters, * and normalize any MIME types or Uris you receive from * outside of Android to ensure these elements are lower case before * supplying them here.</em></p> * * @param type The desired data type to look for, as returned by * Intent.resolveType(). * @param scheme The desired data scheme to look for, as returned by * Intent.getScheme(). * @param data The full data string to match against, as supplied in * Intent.data. * * @return Returns either a valid match constant (a combination of * {@link #MATCH_CATEGORY_MASK} and {@link #MATCH_ADJUSTMENT_MASK}), * or one of the error codes {@link #NO_MATCH_TYPE} if the type didn't match * or {@link #NO_MATCH_DATA} if the scheme/path didn't match. * * @see #match */ public final int matchData(String type, String scheme, Uri data) { final ArrayList<String> types = mDataTypes; final ArrayList<String> schemes = mDataSchemes; int match = MATCH_CATEGORY_EMPTY; if (types == null && schemes == null) { return ((type == null && data == null) ? (MATCH_CATEGORY_EMPTY+MATCH_ADJUSTMENT_NORMAL) : NO_MATCH_DATA); } if (schemes != null) { if (schemes.contains(scheme != null ? scheme : "")) { match = MATCH_CATEGORY_SCHEME; } else { return NO_MATCH_DATA; } final ArrayList<PatternMatcher> schemeSpecificParts = mDataSchemeSpecificParts; if (schemeSpecificParts != null) { match = hasDataSchemeSpecificPart(data.getSchemeSpecificPart()) ? MATCH_CATEGORY_SCHEME_SPECIFIC_PART : NO_MATCH_DATA; } if (match != MATCH_CATEGORY_SCHEME_SPECIFIC_PART) { // If there isn't any matching ssp, we need to match an authority. final ArrayList<AuthorityEntry> authorities = mDataAuthorities; if (authorities != null) { int authMatch = matchDataAuthority(data); if (authMatch >= 0) { final ArrayList<PatternMatcher> paths = mDataPaths; if (paths == null) { match = authMatch; } else if (hasDataPath(data.getPath())) { match = MATCH_CATEGORY_PATH; } else { return NO_MATCH_DATA; } } else { return NO_MATCH_DATA; } } } // If neither an ssp nor an authority matched, we're done. if (match == NO_MATCH_DATA) { return NO_MATCH_DATA; } } else { // Special case: match either an Intent with no data URI, // or with a scheme: URI. This is to give a convenience for // the common case where you want to deal with data in a // content provider, which is done by type, and we don't want // to force everyone to say they handle content: or file: URIs. if (scheme != null && !"".equals(scheme) && !"content".equals(scheme) && !"file".equals(scheme)) { return NO_MATCH_DATA; } } if (types != null) { if (findMimeType(type)) { match = MATCH_CATEGORY_TYPE; } else { return NO_MATCH_TYPE; } } else { // If no MIME types are specified, then we will only match against // an Intent that does not have a MIME type. if (type != null) { return NO_MATCH_TYPE; } } return match + MATCH_ADJUSTMENT_NORMAL; }