首页 文章

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

提问于
浏览
0

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

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

我怀疑这可能是从浏览器,但需要确定 .

谢谢 .

1 回答

  • 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

相关问题