Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test whether intent is available

I'm looking for a way to test whether I can open a certain intent. I know how to test for whether an action is available like in this example. However that's not good enough as an action like Intent.ACTION_VIEW may open different applications depending on the uri presented.

Point in case:

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url);
startActivity(i);

This will open a different app for different urls, such as:

url = "ftp://192.168.1.1"
url = "http://192.168.1.1"

The http url is pretty safe: that will open in the default web browser. The ftp url not so, as it is not sure that users have an ftp client installed (for my case I'm assuming AndFTP as client as it appears to be one of the most popular clients, and there is no standard way to send un/pw to the client so this has to be app-specific).

So now I'd like to test not only if the action is available (the linked code will return true if http is available but ftp not), but also whether there is an app that can handle the specific variety. Testing for the specific app would also be acceptable.

And finally this should be incorporated in a piece of code that gets one or more URIs to choose from, which should open the first possible, and return an error code if no URIs can be opened. Something like this pseudo-code:

success = false;
for (Uri uri : uris) {
   if (actionAvailable(uri)) {
        // set up intent
        startActivity(intent);
        success = true;
        break;
    }
}
return success;
like image 407
Wouter Avatar asked Jul 04 '11 16:07

Wouter


1 Answers

So now I'd like to test not only if the action is available (the linked code will return true if http is available but ftp not), but also whether there is an app that can handle the specific variety.

As @mibollma indicates, use PackageManager and queryIntentActivities(), supplying it the Intent that you want to use with startActivity(), complete with your URL. If queryIntentActivities() returns an empty list, you know there is no match for that Intent. For example, if nothing has an <intent-filter> supporting the ftp scheme, you will get an empty list.

for my case I'm assuming AndFTP as client as it appears to be one of the most popular clients, and there is no standard way to send un/pw to the client so this has to be app-specific

Then do not support FTP. Or, embed your own FTP support rather than relying upon a probably non-existent, incompatible third-party app.

like image 95
CommonsWare Avatar answered Oct 31 '22 22:10

CommonsWare