检测是否从浏览器安装了Google play服务



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

我有一个要求重定向用户谁在托管网页登陆到谷歌play商店的特定应用程序页面,或者,如果谷歌play没有安装,那么我需要打开一个网页浏览器链接到应用程序。

我怀疑这在浏览器上是可能的,但需要确定。

谢谢。

正如我在你的问题标签中看到的,你正在使用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 Services,调用这个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 Service检查器:https://stackoverflow.com/a/19955415/1956278

最新更新