2010-12-14 88 views
8

的包名,我需要檢查是否安裝了這樣的什麼是Android Market或谷歌企業應用套件

/* 
    * 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; 
    } 

Android的市場,但我不知道是否com.google.process.gapps是包有android市場或沒有。

回答

19

它是com.android.vending(在我的Galaxy S上),以下是通過查詢誰處理市場:URI的更好方法。

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

如果列表中至少有一個條目,那麼Market就在那裏。

+0

做我需要改變FOO一個真正的包? – jax 2010-12-14 15:58:27

+5

我並不完全同意這一點...手機上可能有其他與Google無關的市場應用程序,這些市場應用程序也可以處理市場://意圖。如果您需要特別檢查Google的市場應用程序,我認爲您可能需要以某種方式檢查包管理器中的com.android.vending。 – greg7gkb 2012-01-06 00:02:39

1

你的代碼是正確的只需要小的改動

退房代碼修改如下:

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"); 
    } 
相關問題