检测是否已从浏览器安装Google Play服务

时间:2014-03-21 06:23:49

标签: android google-chrome cordova google-play

是否可以检测是否从浏览器安装了Google Play服务?

我需要将登陆托管网页的用户重定向到Google Play商店中的特定应用页面,或者如果未安装Google Play,则需要打开带有指向该网页的链接的网络浏览器。应用

我怀疑这是否可以从浏览器中获得,但需要确定。

感谢。

1 个答案:

答案 0 :(得分:1)

正如我在您的问号中看到的那样,您正在使用Cordova,因此您可以创建一个Javascript界面​​来运行HTML代码中的本机代码。

首先,将以下包导入MainActivity:

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

包括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");
}

然后,在public void OnCreate()之后,插入此函数:

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;

    }
}

在您的HTML代码中,当您需要检测Google Play服务时,请调用此Javascript函数(例如):

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';
}

我已经尝试过此代码并且可以正常运行。我从这里获得了Google Play服务检查程序:https://stackoverflow.com/a/19955415/1956278

相关问题