测试意图是否可用 [英] Test whether intent is available

查看:89
本文介绍了测试意图是否可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来测试我是否可以打开某个意图。我知道如何测试操作是否可用,例如在此示例中。但是,这还不够好,例如Intent之类的动作。ACTION_VIEW可能会根据所显示的uri打开不同的应用程序。

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.

要点在于:

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"

http网址非常安全:它将在默认的Web浏览器中打开。ftp网址并非如此,因为不确定用户是否安装了ftp客户端(对于我而言)我假设AndFTP作为客户端,因为它似乎是最受欢迎的客户端之一,并且没有将un / pw发送到客户端的标准方法,因此这必须是特定于应用程序的。)

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).

所以现在我不仅要测试操作是否可用(如果http可用,而ftp不可用,则链接的代码将返回true),还要测试是否有ap。可以处理特定品种的p。

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.

最后,应将其合并到一段代码中,以获取一个或多个URI,然后打开该URI。第一种可能,如果无法打开URI,则返回错误代码。像这样的伪代码:

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;


推荐答案


所以我现在d不仅要测试操作是否可用(如果http可用,则链接的代码将返回true,而ftp不可用),还想测试是否存在可以处理特定种类的应用。

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.

如@mibollma所示,使用 PackageManager queryIntentActivities(),并提供您要与 startActivity()一起使用的 Intent ,并附上您的URL。如果 queryIntentActivities()返回一个空列表,则说明该 Intent 没有匹配项。例如,如果没有任何东西支持 ftp < intent-filter> ,您将得到一个空列表

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.


我假设AndFTP作为客户端,因为它似乎是最受欢迎的客户端之一,并且没有标准向客户端发送un / pw的方法,因此必须特定于应用程序

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

然后不支持FTP。或者,嵌入自己的FTP支持,而不是依赖可能不存在的,不兼容的第三方应用程序。

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

这篇关于测试意图是否可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆