如何检查是否可以从某些活动处理意图?
到目前为止,我有这个方法,但是就像缺less某些东西一样
例如我有一个文件/ SD卡/声音.3ga,返回false(就像没有活动,可以处理这种types的文件),但是当我从文件pipe理器打开它与媒体播放器没有问题
我认为这个意图不是完整的,我需要更多的东西来确保handlerExistsvariables只有在没有可以处理这个意图的活动
PackageManager pm = getPackageManager(); Intent intent = new Intent(android.content.Intent.ACTION_VIEW); String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uriString)).toString()); String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); intent.setDataAndType(Uri.fromFile(new File(uriString)),mimetype); boolean handlerExists = intent.resolveActivity(pm) != null;
PackageManager manager = context.getPackageManager(); List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0); if (infos.size() > 0) { //Then there is an Application(s) can handle your intent } else { //No Application can handle your intent }
你有没有试过这个意图?
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(yourFileHere));
edwardxu的解决scheme完全适合我。
只是澄清一下:
PackageManager packageManager = getActivity().getPackageManager(); if (intent.resolveActivity(packageManager) != null) { startActivity(intent); } else { Log.d(TAG, "No Intent available to handle action"); }
if (intent.resolveActivity(pm) == null) { // activity not found }
您可以使用:
public static boolean isAvailable(Context ctx, Intent intent) { final PackageManager mgr = ctx.getPackageManager(); List<ResolveInfo> list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); return list.size() > 0; }