如何检查某个活动是否可以处理意图?

到目前为止,我已经有了这种方法,但是好像缺少了一些东西


例如,我有一个文件/sdcard/sound.3ga,该文件返回false(例如没有活动可以处理此类文件),但是当我从文件管理器中打开它时,它可以与媒体播放器一起正常打开


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


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;


慕仙森
浏览 500回答 3
3回答

MMTTMM

edwardxu的解决方案非常适合我。只是澄清一下:PackageManager packageManager = getActivity().getPackageManager();if (intent.resolveActivity(packageManager) != null) {    startActivity(intent);} else {    Log.d(TAG, "No Intent available to handle action");}

ibeautiful

PackageManager manager = context.getPackageManager();List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);if (infos.size() > 0) {&nbsp; &nbsp; //Then there is an Application(s) can handle your intent} else {&nbsp; &nbsp; //No Application can handle your intent}您是否尝试过此意图?Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(yourFileHere));

慕的地8271018

if (intent.resolveActivity(getPackageManager()) == null) {&nbsp; &nbsp; // No Activity found that can handle this intent.&nbsp;}else{&nbsp; &nbsp; // There is an activity which can handle this intent.&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android