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;
}
}
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';
}
1 回答
正如我在您的问题标签中看到的那样,您正在使用Cordova,因此您可以创建一个Javascript接口来运行HTML代码中的本机代码 .
首先,将以下包导入MainActivity:
包括
super.loadUrl()
之后的最后一行:然后,在
public void OnCreate()
之后,插入此函数:在您的HTML代码中,当您需要检测Google Play服务时,请调用此Javascript函数(例如):
我从这里获得了Google Play服务检查程序:https://stackoverflow.com/a/19955415/1956278