我可以测试一下应用程序是否可以处理意图,而无需启动它吗? [英] Can I test to see if an application is avaliable to handle an intent, without starting it?

查看:63
本文介绍了我可以测试一下应用程序是否可以处理意图,而无需启动它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体来说,我正在尝试确定是否有可以处理市场意图的应用程序,但我需要一个通用的解决方案.

Specifically, I'm trying to figure out if there is an application to handle the market intent, but I'd like a general case solution.

我知道您是否执行了这样的操作,您可以判断是否有可用的应用程序来处理此意图.我正在尝试做的事情实际上并没有启动意图.关于我可能会做什么的任何想法?

I know if you do something like this, you can tell if there is an application available to handle the intent. I'm trying to do something that doesn't actually launch the intent. Any ideas on what I might do?

try
{
    String strURL="market://details?id="+thePackage;
    Intent the_intent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
    the_intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
    startActivity(the_intent)

}
catch (ActivityNotFoundException e)
{
    String strUrl="https://play.google.com/store/search?c=apps&q="+thePackage;

    Intent the_intent = new Intent(Intent.ACTION_VIEW, Uri.parse(strUrl));
    the_intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
    startActivity(the_intent)
}

我想要的是一个不涉及实际启动活动的解决方案.我要这样做的原因有很多,但我想我不是唯一想到这一点的人,似乎必须有办法...

What I want is a solution that doesn't involve actually starting the activity. There's a few reasons why I want to do this, but I imagine I'm not the only one to have thought of a reason for this, it seems like there must be a way...

推荐答案

使用PackageManagerqueryIntentActivities()resolveActivity().前者将返回与Intent匹配并与startActivity()一起使用的事物的List.后者将返回没有匹配的null或最佳匹配"的Intent(如果有多个匹配,则可能是选择器活动).

Use PackageManager and queryIntentActivities() or resolveActivity(). The former will return a List of things that match an Intent to be used with startActivity(). The latter will return null for no matches or an Intent which is the "best match" (which could be the chooser activity if there is more than one match).

这篇关于我可以测试一下应用程序是否可以处理意图,而无需启动它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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