13

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://.

Thomas Ahle
  • 30,774
  • 21
  • 92
  • 114
  • Is it possible to get data from the imdb activity? I mean, it could be great to get for example the rating of the movie, or somehting else. – Nuno Gonçalves Jun 24 '12 at 12:14

2 Answers2

16

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.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • I tried `Intent test = new Intent(Intent.ACTION_VIEW, Uri.parse("imdb://find"));` `getPackageManager().resolveActivity(test, 0);`, but it didn't even get a mention in the adb log. – Thomas Ahle Aug 13 '11 at 22:55
  • It might not be mentioned in logs, its up to the implementation to decide. However you may add your own logs. Just print info returned in `ResolveInfo` instance from `resolveActivity()` yourself. – inazaruk Aug 14 '11 at 00:38
  • Thanks, I thought it was gonna throw an exception. Works like a charm. – Thomas Ahle Aug 14 '11 at 07:34
0

Following worked for me:

    Intent intent = new Intent();
    intent.setAction("android.intent.action.VIEW");
    Uri uri = Uri.parse("imdb:///title/tt1457767/");
    intent.setData(uri);
    this.getActivity().startActivity(intent);

You can replace String in Uri.parse("");with any of followings:

            imdb:///name/<nameID>
            imdb:///find?q=<search_query>
            imdb:///title/<titleID>
            imdb:///boxoffice
            imdb:///name/<nameID>
            imdb:///find?q=toystory
Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61