9

I need to check if the Android Market is installed like this

    /*
     * Test for existence of Android Market
     */
    boolean androidMarketExists = false;
    try{
        ApplicationInfo info = getPackageManager()
                             .getApplicationInfo("com.google.process.gapps", 0 );
        //application exists
        androidMarketExists = true;
    } catch( PackageManager.NameNotFoundException e ){
        //application doesn't exist
        androidMarketExists = false;
    }

But I don't know if com.google.process.gapps is the package that has android market or not.

jax
  • 37,735
  • 57
  • 182
  • 278

2 Answers2

19

It's com.android.vending (on my Galaxy S), and here's the better way to find out... by querying for who handles market:// URIs.

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("market://search?q=foo"));
    PackageManager pm = getPackageManager();
    List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);

If the list has at least one entry, the Market's there.

Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86
  • do I need to change foo to a real package? – jax Dec 14 '10 at 15:58
  • 6
    I don't totally agree with this... there could be other market apps on the phone that have no relation to Google, and these could also handle market:// intents. If you need to check for Google's market app specifically, I think you might need to check for com.android.vending in the package manager somehow. – greg7gkb Jan 06 '12 at 00:02
1

Your code is right just needs minor changes

Check out code modified below:

boolean androidMarketExists = false;
    try{
        ApplicationInfo info = getPackageManager().getApplicationInfo("com.android.vending", 0 );
        if(info.packageName.equals("com.android.vending"))
            androidMarketExists = true;
        else
            androidMarketExists = false;
    } catch(PackageManager.NameNotFoundException e ){
        //application doesn't exist
        androidMarketExists = false;
    }
    if(!androidMarketExists){
        Log.d(LOG_TAG, "No Android Market");
        finish();
    }
    else{
        Log.d(LOG_TAG, "Android Market Installed");
    }
Akshay Chordiya
  • 4,761
  • 3
  • 40
  • 52