Can I test to see if an application is avaliable t

2019-02-18 12:24发布

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

2条回答
不美不萌又怎样
2楼-- · 2019-02-18 12:43

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

查看更多
淡お忘
3楼-- · 2019-02-18 13:01

This is a practical example from CommonWare's answer.

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 );
Log.v(TAG,""+_context.getPackageManager().queryIntentActivities(the_intent,Intent.FLAG_ACTIVITY_NEW_TASK));
if (_context.getPackageManager().queryIntentActivities(the_intent,0).size()==0)
{
    String strUrl="https://play.google.com/store/search?c=apps&q="+thePackage;
    the_intent = new Intent(Intent.ACTION_VIEW, Uri.parse(strUrl));
    the_intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
}
查看更多
登录 后发表回答