public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE 最低Google Play服务包版本(在AndroidManifest.xml android:versionCode中声明),以便与此客户端版本兼容 . 常数值:3225000(0x003135a8)
private boolean checkGooglePlayServices() {
final int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status != ConnectionResult.SUCCESS) {
Log.e(TAG, GooglePlayServicesUtil.getErrorString(status));
// ask user to update google play services.
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, 1);
dialog.show();
return false;
} else {
Log.i(TAG, GooglePlayServicesUtil.getErrorString(status));
// google play services is updated.
//your code goes here...
return true;
}
}
private void checkPlayService(int PLAY_SERVICE_STATUS)
{
switch (PLAY_SERVICE_STATUS)
{
case ConnectionResult.API_UNAVAILABLE:
//API is not available
break;
case ConnectionResult.NETWORK_ERROR:
//Network error while connection
break;
case ConnectionResult.RESTRICTED_PROFILE:
//Profile is restricted by google so can not be used for play services
break;
case ConnectionResult.SERVICE_MISSING:
//service is missing
break;
case ConnectionResult.SIGN_IN_REQUIRED:
//service available but user not signed in
break;
case ConnectionResult.SUCCESS:
break;
}
}
我这样用它,
GoogleApiAvailability avail;
int PLAY_SERVICE_STATUS = avail.isGooglePlayServicesAvailable(this);
checkPlayService(PLAY_SERVICE_STATUS);
int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
if(status != ConnectionResult.SUCCESS) {
if(status == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){
Toast.makeText(this,"please update your google play service",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "please download the google play service", Toast.LENGTH_SHORT).show();
}
}
14
IMPORTANT :GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE返回您应用所需的最低版本,而不是用户设备上安装的播放服务版本 .
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
FragmentManager mgr = getFragmentManager();
MapFragment mapFragment = (MapFragment)mgr.findFragmentById(R.id.map);
mMap = mapFragment.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// The Map is verified. It is now safe to manipulate the map.
setUpMap();
}else{
// check if google play service in the device is not available or out-dated.
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
// nothing anymore, cuz android will take care of the rest (to remind user to update google play service).
}
}
}
11 回答
我发现简单的解决方案
但
versionCode
在API 28中已弃用,因此您可以使用PackageInfoCompat
:如果你查看Stan0提供的链接,你会看到this:
因此,当您在清单中设置它然后调用isGooglePlayServicesAvailable(context)时:
它将确保设备使用您的应用所需的版本,如果没有,那么您可以根据文档采取措施
编辑
不推荐使用
GooglePlayServicesUtil.isGooglePlayServicesAvailable()
,现在应该使用GoogleApiAvailability.isGooglePlayServicesAvailable() .这是我的解决方案:
并且您有两个选项,或者直接在else块中编写代码作为注释,或者使用返回的boolean值来编写自定义代码 .
我希望这段代码可以帮助别人 .
从最新的更新我已经得到这个代码,我已经做了一个方便的方法来管理与它相关的所有东西..
有关服务可用性和相关详细信息的所有详细信息均可使用here .
我这样用它,
对于版本
GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE;
会给你 .我在研究期间找到的最有用的答案之一is here.
IMPORTANT :GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE返回您应用所需的最低版本,而不是用户设备上安装的播放服务版本 .
今天早上我遇到了同样的问题 . 并且通过stan0的建议完成了它 . 谢谢stan0 .
根据https://developers.google.com/maps/documentation/android/map中的示例代码,只需要进行一项更改 .
此外,您需要将一个属性添加到manifest.xml中:
“3225130”的值取自您项目正在使用的google-play-services_lib . 此外,该值位于google-play-services_lib中manifest.xml的相同位置 .
希望它会有所帮助 .
如果您在应用程序管理器中查找Google Play服务,则会显示已安装的版本 .
使用以下功能检查是否可以使用Google Play服务
将获得与以下相同的结果:
但是第二个需要放在试一试 .