How to check if an intent can be handled from some activity?

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

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

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;
73890 次浏览
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 intent?

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(yourFileHere));
if (intent.resolveActivity(getPackageManager()) == null) {
// No Activity found that can handle this intent.
}
else{
// There is an activity which can handle this intent.
}

edwardxu's solution works perfectly for me.

Just to clarify a bit:

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

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;
}

Using Kotlin If you need to do something when intent is not available,

fun isIntentAvailable(context: Context, action: String?): Boolean {
val packageManager = context.packageManager
val intent = Intent(action)
val resolveInfo: List<*> = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
return resolveInfo.isNotEmpty()
}

Implement this method as

private const val SAMPLE_INTENT = "com.realwear.barcodereader.intent.action.SCAN_BARCODE"


if(isIntentAvailable(this,SAMPLE_INTENT)){
//Do Stuff
}

If you don't have anything to do,

Intent(SAMPLE_INTENT).also { barcodeReaderIntent ->
barcodeReaderIntent.resolveActivity(packageManager)?.also {
barcodeReaderIntent.putExtra(EXTRA_CODE_128, false)
startActivityForResult(barcodeReaderIntent, BARCODE_REQUEST_CODE)
}
}

Another approach using kotlin extension

 fun Context.isIntentAvailable(intent: Intent): Boolean {
val resolveInfo: List<*> = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
return resolveInfo.isNotEmpty()
}

Usage inside fragment

val available = requireContext().isIntentAvailable(this)

Usage inside activity

val available = this.isIntentAvailable(this)

The Clear Answer to solve this problem

if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}

Small update for one looking for something similar in 2021 :) Since Android 11 it is not desired to call the package manager, so extra efforts are needed. So why don't you just wrap startActivity() in try-catch? Or even better - use Rx beautiful error handing:

....
.flatMapCompletable {
doSomethingThrowable()
.onErrorResumeNext { completableCallbackIfNotResolvable() }
}
.subscribe()

Here you are another Kotlin solution. It works perfectly even for Android 30+. First include this in you app manifest:

<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="choose-scheme-goes-here" />
</intent>
</queries>

Then you can check if an intent can be handled with this function:

private fun isIntentAvailable(intent: Intent): Boolean {
val manager = requireContext().packageManager
val info = manager.queryIntentActivities(intent, 0)
return info.size > 0
}