How to check if any intention can be handled from any activity?

I have this method so far, but it appeared as if something was missing

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

I think this intention is not complete, and I need something else to make sure that the variable handlerExists will be false ONLY if there is no activity that can handle this intention.

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; 
+69
android mime-types android-intent file-type intentfilter
Mar 14 '13 at 11:07
source share
4 answers

Edwardxu solution works great for me.

Just a little clarification:

 PackageManager packageManager = getActivity().getPackageManager(); if (intent.resolveActivity(packageManager) != null) { startActivity(intent); } else { Log.d(TAG, "No Intent available to handle action"); } 
+76
May 01 '15 at 20:17
source share
 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 } 

Have you tried this intention?

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(yourFileHere)); 
+71
Mar 14 '13 at 11:15
source share
 if (intent.resolveActivity(pm) == null) { // activity not found } 
+19
Feb 16 '15 at 2:51
source share

You can use:

 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; } 
+3
Oct. 16 '15 at 15:55
source share



All Articles