Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if intent uri is available

I have an app that sometimes links to imdb. I can do this with http://m.imdb.com/find or if it is available imdb:///find. My trouble is I can't find a way to check whether the imdb:// is available before it is too late.

Because the link is clicked on by the user, I can't catch the android.content.ActivityNotFoundException exception.

Am I missing something obvious?


Solution:

By inazaruk's suggestion I'm now using the follwing code:

public static boolean isUriAvailable(Context context, String uri) {
    Intent test = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    return context.getPackageManager().resolveActivity(test, 0) != null;
}

The method must be called with a direct uri to what you are requesting, like imdb:///find rather than just imdb://.

like image 991
Thomas Ahle Avatar asked Aug 13 '11 11:08

Thomas Ahle


1 Answers

You can use resolveActivity() to detect whether there is any Activity in the system capable of handling your intent. Of course you need to construct the intent that mimics the "uri" click. That might be done using Intent.setData() function, though I didn't test/verify that myself.

like image 128
inazaruk Avatar answered Sep 20 '22 18:09

inazaruk