在我的应用程序中,我需要检查用户设备中安装的Google Play服务版本号。这可行吗?如果可以,如何实现?
在我的应用程序中,我需要检查用户设备中安装的Google Play服务版本号。这可行吗?如果可以,如何实现?
我找到了一个简单的解决方案:
int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode;
但是在API 28中versionCode
已被弃用,因此您可以使用PackageInfoCompat
:
long v = PackageInfoCompat.getLongVersionCode(getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ));
public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE
为了与此客户端版本兼容,必须在AndroidManifest.xml文件中声明最小的Google Play服务包版本(android:versionCode)。常量值为3225000(0x003135a8)。
因此,在您的清单文件中设置了这个版本号后,调用isGooglePlayServicesAvailable(context)
函数:
public static int isGooglePlayServicesAvailable (Context context)
该方法验证此设备上已安装并启用Google Play服务,并且在此设备上安装的版本不早于此客户端所需的版本。
返回值
SUCCESS
、SERVICE_MISSING
、SERVICE_VERSION_UPDATE_REQUIRED
、SERVICE_DISABLED
、SERVICE_INVALID
。如果设备未使用您的应用程序要求的版本,则此方法将确保其使用所需的版本。否则,您可以根据文档采取相应措施。
GooglePlayServicesUtil.isGooglePlayServicesAvailable()
已过时,现在应改为使用GoogleApiAvailability.isGooglePlayServicesAvailable()
。
这是我的解决方案:
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;
}
}
你有两个选择,要么在else块中直接编写代码(以注释形式),要么使用此方法返回的布尔值编写自定义代码。
重要提示:GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE 返回的是您的应用所需的最低版本,而不是用户设备上安装的Play服务版本。
从最新的更新中,我得到了这段代码,我已经制作了一个方便的方法来管理与其相关的所有内容。
有关服务可用性和相关详细信息,请单击此处查看。
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);
对于版本号GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE;
将为您提供相应信息。
我在研究过程中发现最有用的答案之一在这里。
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();
}
}
int apkVersion = GoogleApiAvailability.getInstance().getApkVersion(getContext());
将得到与以下代码相同的结果:
int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode;
更新(2019.07.03)Kotlin版本:
class GooglePlayServicesUtil {
companion object {
private const val GOOGLE_PLAY_SERVICES_AVAILABLE_REQUEST = 9000
fun isGooglePlayServicesWithError(activity: Activity, showDialog: Boolean): Boolean {
val status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity)
return if (status != ConnectionResult.SUCCESS) {
if (showDialog) {
GoogleApiAvailability.getInstance().getErrorDialog(activity, status, GOOGLE_PLAY_SERVICES_AVAILABLE_REQUEST).show()
}
true
} else {
false
}
}
}
}
今天早上我也遇到了同样的问题。根据stan0的建议解决了问题,感谢stan0。
只需对https://developers.google.com/maps/documentation/android/map的示例代码进行一处更改即可。
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).
}
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name"
android:versionCode="3225130"
android:versionName="your.version" >
“3225130”这个值是从你的项目中使用的google-play-services_lib中获取的。此外,该值与google-play-services_lib中manifest.xml的同一位置相关联。