如何检查是否可以从某些活动中处理意图? [英] How to check if an intent can be handled from some activity?

查看:20
本文介绍了如何检查是否可以从某些活动中处理意图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止我有这个方法,但它好像缺少了什么

I have this method so far , but it came up like something is missing

例如,我有一个返回 false 的文件/sdcard/sound.3ga(就像没有活动可以处理这种类型的文件),但是当我从文件管理器打开它时,它与媒体播放器一起打开,没有问题

for example I have a file /sdcard/sound.3ga that returns false ( like there is no activity that can handle this type of file ) , But when I open it from the file manager it opens with the media player with no problem

我认为这个意图并不完整,我需要做更多的事情来确保只有当没有可以处理这个意图的活动时,handlerExists 变量才会为假

I think this intent is not complete and I need to to something more to make my self sure that the handlerExists variable will be false ONLY if there is no activity that can handle this intent

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;

推荐答案

edwardxu 的解决方案非常适合我.

edwardxu's solution works perfectly for me.

只是为了澄清一点:

PackageManager packageManager = getActivity().getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
    startActivity(intent);
} else {
    Log.d(TAG, "No Intent available to handle action");
}

这篇关于如何检查是否可以从某些活动中处理意图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆