【发布时间】:2014-03-22 17:56:12
【问题描述】:
是否可以检测是否从浏览器安装了 Google Play 服务?
我需要将登陆托管网页的用户重定向到 Google Play 商店中的特定应用程序页面,或者,如果未安装 Google Play,则我需要打开带有指向应用程序。
我怀疑这甚至可以通过浏览器实现,但需要确定。
谢谢。
【问题讨论】:
标签: android google-chrome cordova google-play
是否可以检测是否从浏览器安装了 Google Play 服务?
我需要将登陆托管网页的用户重定向到 Google Play 商店中的特定应用程序页面,或者,如果未安装 Google Play,则我需要打开带有指向应用程序。
我怀疑这甚至可以通过浏览器实现,但需要确定。
谢谢。
【问题讨论】:
标签: android google-chrome cordova google-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 服务检查器:https://stackoverflow.com/a/19955415/1956278
【讨论】: