tomisyourname
tomisyourname

Reputation: 91

How to check if an android device has Google Play installed?

I am trying to check if the device has Google Play installed or not in my app, but seems there is no way to do that. I followed the post HERE but still doesn't work, always return true even i was testing with an emulator, it has com.android.vending installed. So am i checking the wrong package name? Any ideas for that?

Thanks in advance!

Upvotes: 1

Views: 2710

Answers (3)

AllanRibas
AllanRibas

Reputation: 1184

In my case, I use an extended function

fun Context.isPackageInstalled(packageName: String): Boolean {
    return try {
        packageManager.getPackageInfo(packageName, 0)
        true
    } catch (e: PackageManager.NameNotFoundException) {
        false
    }
}
fun customLog(myTag: String, message: String)  {
    val caller = getCallerInfo(Throwable().stackTrace)
    println("customLog $myTag $message $caller")
}

Call in your activity

val isGooglePlayServicesAvailable = isPackageInstalled("com.android.vending")
if (isGooglePlayServicesAvailable){
    customLog(myTag, "GooglePlayServicesIsAvailable()")
}         
else {
    customLog(myTag, "GooglePlayServicesNotAvailable()")
}

Upvotes: 0

tomisyourname
tomisyourname

Reputation: 91

Finally, found a way to check Google Play installed, here is the code:

public static boolean isPackageInstalled(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 = (!TextUtils.isEmpty(label) && label.startsWith("Google Play"));
    } catch(PackageManager.NameNotFoundException e) {
       app_installed = false;
    }
    return app_installed;
}

Upvotes: 2

Dinesh Sharma
Dinesh Sharma

Reputation: 11571

Follow Dcoumentation to check if the device has Google Play Service available.

In Short, simply:

// Getting status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

// Showing status
if(status==ConnectionResult.SUCCESS)
//Google Play Services are available
else{
//Google Play Services are not available
}

Hope this will help you :)

Upvotes: 3

Related Questions