How to detect if Google Play is installed? (Not Market)

前端 未结 5 1196
[愿得一人]
[愿得一人] 2021-01-02 20:43

Whether the app installed is called Google Play or Market, the package name is the same com.android.vending.

I need to be able to detect whether the app

相关标签:
5条回答
  • 2021-01-02 20:47

    You can use this simple piece of code, its easy and to the point with a consideration for not re-inventing the wheel using GooglePlayServicesUtil:

    public static boolean isPlayStoreInstalled(Context context){
    try {
        context.getPackageManager()
                .getPackageInfo(GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE, 0);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
    }
    

    This will require you to add this to your dependencies:

    compile 'com.google.android.gms:play-services-base:[PLAY_SERVICES_VERSION]'
    

    Latest play-services version is now: 10.0.1

    0 讨论(0)
  • 2021-01-02 20:48

    You can also try this much simplified solution:

    public boolean isGooglePlayAvailable() {
            boolean googlePlayStoreInstalled;
            int val= GooglePlayServicesUtil.isGooglePlayServicesAvailable(LocationActivity.this);
            googlePlayStoreInstalled = val == ConnectionResult.SUCCESS;
            return googlePlayStoreInstalled;
        }
    
    0 讨论(0)
  • 2021-01-02 20:48

    This is probably a better example as it allows for status' where the user can do something about it i.e re-auth or update. Based on the code in the GCM client example project:

     public static boolean checkPlayServices(Activity activity) {
            int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);
            if (resultCode != ConnectionResult.SUCCESS) {
                if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                    GooglePlayServicesUtil.getErrorDialog(resultCode, activity,
                            PLAY_SERVICES_RESOLUTION_REQUEST).show();
                } else {
                    Toast.makeText(activity.getApplicationContext(), "This device is not supported.", Toast.LENGTH_LONG).show();
                    activity.finish();
                }
                return false;
            }
            return true;
        }
    
    0 讨论(0)
  • 2021-01-02 20:50

    In my App I check possibility to open play store before fire it like:

        public static boolean isResolveActivity(Intent intent) {
                return App.getInstance().getPackageManager().resolveActivity(intent, PackageManager.GET_RESOLVED_FILTER) != null;
            }
    
       public void isResolveActivity(String appPackage) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackage));
    
          if(isResolveActivity(intent)){
          ...open intent
          }
      }
    
    0 讨论(0)
  • 2021-01-02 20:57

    I figured out how to check the application label. I was using the debugger to see what all was being returned in packageInfo that's why I didn't see it initially.

    public static boolean isGooglePlayInstalled(Context context) {
        PackageManager pm = context.getPackageManager();
        boolean app_installed = false;
        try
        {
               PackageInfo info = pm.getPackageInfo("com.android.vending", PackageManager.GET_ACTIVITIES);
               String label = (String) info.applicationInfo.loadLabel(pm);
               app_installed = (label != null && !label.equals("Market"));
        }
        catch (PackageManager.NameNotFoundException e)
        {
               app_installed = false;
        }
        return app_installed;
    }
    
    0 讨论(0)
提交回复
热议问题