Programmatically check if android device has Google Play?
Asked Answered
R

2

7

Currently there are some devices that doesn't have Google Play (Huawei Devices), is there a way for me to check programmatically if the device doesn't have one? I wanted to use the in-app-update feature: https://developer.android.com/guide/playcore/in-app-updates and check if the device has a Google PlayStore first.

What I have tried so far: using Uri.parse("market://details?id=" + appPackageName) but you need to start the activity first to confirm it exists? I'd like to know if there are better ways to do it.

Recite answered 8/7, 2020 at 4:23 Comment(0)
S
5

You can check if the package com.android.vending exists or not

public static boolean isPackageInstalled(String packageName, PackageManager packageManager) {
    try {
        return packageManager.getApplicationInfo(packageName, 0).enabled;
    }
    catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

And you can do like this in your activity


if(isPackageInstalled("com.android.vending", getPackageManager())){ 

   // play store is installed

}else{ 

   // play store is not installed

}

Struggle answered 8/7, 2020 at 4:34 Comment(1)
what different between if and else?Carolann
R
9

You can use the following code to check whether the android device has Google Play:

boolean flag = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this) == com.google.android.gms.common.ConnectionResult.SUCCESS

If flag == true, then GMS is enable in device.

If flag == false, then GMS is disable in device.

Rehash answered 8/7, 2020 at 8:22 Comment(2)
is this code blocking and need another thread or we can use that code on the main thread?Pater
@AbdulmalekDery Can be used on main thread, although the best practice is to avoid calling on main thread anything that specifically does not need to run on main thread.Hyphenate
S
5

You can check if the package com.android.vending exists or not

public static boolean isPackageInstalled(String packageName, PackageManager packageManager) {
    try {
        return packageManager.getApplicationInfo(packageName, 0).enabled;
    }
    catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

And you can do like this in your activity


if(isPackageInstalled("com.android.vending", getPackageManager())){ 

   // play store is installed

}else{ 

   // play store is not installed

}

Struggle answered 8/7, 2020 at 4:34 Comment(1)
what different between if and else?Carolann

© 2022 - 2024 — McMap. All rights reserved.