How can I check if the Android Market is installed on my user's device?
How could I write a code which can tell me that android market is开发者_JAVA技巧 installed on your android phone?
There are two ways. You can use the already mentioned getPackageManager()
and getApplicationInfo()
(if the package is not found, a PacketManager.NameNotFoundException
will be thrown - see here). Android Market's package name is com.android.vending
.
However, you can also create a dummy intent for searching the market and check how it is handled. If the resulting list has at least one entry, you can be sure that Android Market is installed:
Intent market = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=dummy"));
PackageManager manager = getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(mmarket, 0);
Here's what I did (assumes browser exists):
Intent market = new Intent(Intent.ACTION_VIEW).setData(Uri
.parse("market://details?id=com.example.app"));
Intent website = new Intent(Intent.ACTION_VIEW).setData(Uri
.parse("http://play.google.com/store/apps/details?id=com.example.app"));
try {
startActivity(market);
} catch (ActivityNotFoundException e) {
startActivity(website);
}
精彩评论