How to detect the existence of Android Market on d

2019-04-12 04:40发布

问题:

Some Android devices don't have Android Market, like Korea, etc.

Is it possible to detect the existence of Android Market at runtime?

I know I can try to open a market uri first to see if there is any exception thrown. But I don't think this is a wise approach.

回答1:

I know I can try to open a market uri first to see if there is any exception thrown.

Create the ACTION_VIEW Intent for the Market Uri, then use PackageManager and queryIntentActivities() to see if you get anything back. If that returns an empty list, you know nothing on the device handles Market Uri values.



回答2:

A complete solution, that also reads the packagename by itself ...

Context context = this;
final PackageManager packageManager = context.getPackageManager();

String packagename = this.getPackageName();

String url = "market://details?id=" + packagename;

Intent i2 = new Intent(Intent.ACTION_VIEW);
i2.setData(Uri.parse(url));

List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(i2,PackageManager.MATCH_DEFAULT_ONLY);

if (resolveInfo.size() > 0) startActivity(i2);