PackageManager-->resolveActivity查询是否有符合条件的Activity

本文介绍了如何使用Intent.ACTION_VIEW启动Activity,并通过setDataAndType方法指定要处理的数据类型。此外,还详细解释了PackageManager.resolveActivity方法的工作原理,以及如何确定是否可以处理特定的mimetype。
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), mimetype);
if (getPackageManager().resolveActivity(intent,
PackageManager.MATCH_DEFAULT_ONLY) != null) {
// someone knows how to handle this mime type with this scheme, don't download.
try {
startActivity(intent);
return;
} catch (ActivityNotFoundException ex) {
if (Config.LOGD) {
Log.d(LOGTAG, "activity not found for " + mimetype
+ " over " + Uri.parse(url).getScheme(), ex);
}

}

查询是否有符合Uri.parse(url), mimetype这两个条件的Activity

public abstract ResolveInfo resolveActivity (Intent intent, int flags)

Since: API Level 1
Determine the best action to perform for a given Intent. This is how resolveActivity(PackageManager) finds an activity if a class has not been explicitly specified.
Parameters
intent An intent containing all of the desired specification (action, data, type, category, and/or component).
flags Additional option flags. The most important is MATCH_DEFAULT_ONLY, to limit the resolution to only those activities that support the CATEGORY_DEFAULT.
Returns
Returns a ResolveInfo containing the final activity intent that was determined to be the best action. Returns null if no matching activity was found.
See Also
MATCH_DEFAULT_ONLY
GET_INTENT_FILTERS
GET_RESOLVED_FILTER


resolveContentProvider和resolveService的作用一样。
public static void previewBuiltInLiveWallpaper( @NonNull Activity activity, @NonNull ComponentName component, List<Uri> onlineWallpaperUriList, List<Integer> onlineWallpaperCatList , String selectWallpaperId ) { Log.e("HXKTag","component:["+component.getPackageName()+","+component.getClassName()+"]"); Intent preview = new Intent(PREVIEW_BUILT_IN_LIVE_WALLPAPER); preview.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, component); boolean isOnlineVideoServiceExist = isOnlineVideoServiceExist(activity); if (isOnlineVideoServiceExist) { Log.e("HXKTag","isOnlineVideoServiceExist:"+isOnlineVideoServiceExist); preview.setType("video/mp4"); preview.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } PackageManager pm = activity.getPackageManager(); if ((pm != null) && (preview.resolveActivity(pm) == null)) { String logTag = activity.getLocalClassName(); int lastDotIndex = logTag.lastIndexOf('.'); if ((lastDotIndex != -1) && (lastDotIndex + 1 < logTag.length())) { logTag = logTag.substring(lastDotIndex + 1); } LogUtils.debugD( logTag, "Found no built-in live wallpaper preview page, jump to default page" ); Log.e("HXKTag","Found no built-in live wallpaper preview page, jump to default page"); preview.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER); } if (isOnlineVideoServiceExist) { if (pm != null && preview.resolveActivity(pm) != null) { ClipData clipData = getClipDataForUriList(activity, onlineWallpaperUriList); if (clipData != null) { Log.e("HXKTag","clipData != null"); preview.setClipData(clipData); } if (selectWallpaperId != null) { Log.e("HXKTag","selectWallpaperId != nullselectWallpaperId:"+selectWallpaperId); // preview.putExtra(SELECT_ONLINE_LIVE_WALLPAPER_ID_EXTRA_KEY, selectWallpaperId); } if (onlineWallpaperCatList != null) { Log.e("HXKTag","onlineWallpaperCatList != null"); CharSequence charSequence = onlineWallpaperCatList.stream() .map(i -> (char) i.intValue()) .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append); preview.putExtra(SELECT_ONLINE_LIVE_WALLPAPER_CAT_EXTRA_KEY, charSequence); } Log.e("HXKTag","(pm != null && preview.resolveActivity(pm) != null) End"); } } activity.startActivityForResult(preview, PREVIEW_BUILT_IN_LIVE_WALLPAPER_CODE); }为什么点击下载的动态壁纸不能跳转
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值