RyanP13
RyanP13

Reputation: 7753

Detect if Google Plays services is installed from a browser

Is it possible to detect if Google play services is installed from a browser?

I have a requirement to redirect a user who lands on a hosted web page to either a specific application page in the Google play store or, if Google play is not installed then I need to open a web browser with the link to the app.

I doubt this is even possible from the browser but need to be sure.

Thanks.

Upvotes: 0

Views: 1401

Answers (1)

josemmo
josemmo

Reputation: 7093

As I can see in your question tags, you're using Cordova, so you can create a Javascript Interface to run a native code from your HTML code.

First of all, import the following packages to your MainActivity:

import android.content.Context;
import android.webkit.JavascriptInterface;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

Include the last line after super.loadUrl():

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init();
    super.loadUrl("file:///android_asset/www/index.html");
    [...]
    super.appView.addJavascriptInterface(new WebAppInterface(this), "jsInterface");
}

Then, after public void OnCreate(), insert this function:

public class WebAppInterface {
    Context mContext;
    WebAppInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public boolean isGooglePlayInstalled() {
        boolean googlePlayStoreInstalled;
        int val = GooglePlayServicesUtil.isGooglePlayServicesAvailable(MainActivity.this);
        googlePlayStoreInstalled = val == ConnectionResult.SUCCESS;
        return googlePlayStoreInstalled;

    }
}

In your HTML code, when you need to detect Google Play Services, call this Javascript function (for example):

if (jsInterface.isGooglePlayInstalled()) {
    //Google Play Services detected
    document.location.href = 'http://www.my-awesome-webpage.com/';
} else {
    //No Google Play Services found
    document.location.href = 'market://details?id=com.google.android.gms';
}

I've tried this code and it works. I've got the Google Play Service checker from here: https://stackoverflow.com/a/19955415/1956278

Upvotes: 1

Related Questions