In my application, I need to check Google Play services version (which is installed in user's device). Is it possible ? And if yes, how can I do that? I searched Google but I could not find anything!
在我的应用程序中,我需要检查Google Play服务版本(安装在用户设备中)。可能吗 ?如果是,我该怎么做?我搜索谷歌但我找不到任何东西!
10 个解决方案
#1
80
I found simple solution:
我发现简单的解决方案
int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode;
#2
48
If you look in the link provided by Stan0 you will see this:
如果你查看Stan0提供的链接,你会看到:
public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE
Minimum Google Play services package version (declared in AndroidManifest.xml android:versionCode) in order to be compatible with this client version. Constant Value: 3225000 (0x003135a8)
最低Google Play服务包版本(在AndroidManifest.xml android:versionCode中声明),以便与此客户端版本兼容。常数值:3225000(0x003135a8)
So, when you set that in your manifest and then call isGooglePlayServicesAvailable(context)
:
因此,当您在清单中设置它然后调用isGooglePlayServicesAvailable(context)时:
public static int isGooglePlayServicesAvailable (Context context)
Verifies that Google Play services is installed and enabled on this device, and that the version installed on this device is no older than the one required by this client.
验证是否已在此设备上安装并启用了Google Play服务,并且此设备上安装的版本不早于此客户端所需的版本。
Returns
返回
- status code indicating whether there was an error. Can be one of following in ConnectionResult:
SUCCESS
,SERVICE_MISSING
,SERVICE_VERSION_UPDATE_REQUIRED
,SERVICE_DISABLED
,SERVICE_INVALID
.- 状态代码,指示是否有错误。可以是ConnectionResult中的以下之一:SUCCESS,SERVICE_MISSING,SERVICE_VERSION_UPDATE_REQUIRED,SERVICE_DISABLED,SERVICE_INVALID。
It will ensure that the device is using the version your app requires, if not, then you can take action according to the documentation
它将确保设备使用您的应用所需的版本,如果没有,那么您可以根据文档采取措施
Edit
GooglePlayServicesUtil.isGooglePlayServicesAvailable()
is deprecated and now GoogleApiAvailability.isGooglePlayServicesAvailable()
should be used instead.
不推荐使用GooglePlayServicesUtil.isGooglePlayServicesAvailable(),现在应该使用GoogleApiAvailability.isGooglePlayServicesAvailable()。
#3
13
here is my solution:
这是我的解决方案:
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;
}
}
and you have two options, either write your code directly in else block as commented, or use the returned boolean value for this method to write custom code.
并且您有两个选项,或者直接在else块中编写代码作为注释,或者使用返回的boolean值来编写自定义代码。
I hope this code helps someone.
我希望这段代码可以帮助别人。
#4
4
From newer updates i have ended up with this code, i have made a handy method to manage all stuff related to it..
从更新的更新我已经得到这个代码,我已经做了一个方便的方法来管理与它相关的所有东西..
All details related to availability of Service and related details are available here.
有关服务可用性和相关详细信息的所有详细信息,请参见此处。
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;
}
}
I use it like this,
我这样用,
GoogleApiAvailability avail;
int PLAY_SERVICE_STATUS = avail.isGooglePlayServicesAvailable(this);
checkPlayService(PLAY_SERVICE_STATUS);
And for version GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE;
will give you.
对于版本GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE;会给你。
And one of the most useful answer i found during my research is here.
我在研究期间找到的最有用的答案之一就是这里。
#5
2
IMPORTANT: GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE returns the minimum version required by YOUR app, not the play services version that is installed on the user's device.
重要提示:GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE会返回您应用所需的最低版本,而不是用户设备上安装的播放服务版本。
#6
1
I’ve run into the same problem this morning. And got it done by the advice from stan0. Thanks stan0.
今天早上我遇到了同样的问题。并且通过stan0的建议完成了它。谢谢stan0。
Only one change is needed based on the sample code from https://developers.google.com/maps/documentation/android/map.
根据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).
}
}
}
Besides, you need to add an attribute to your manifest.xml as:
此外,您需要将一个属性添加到manifest.xml中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name"
android:versionCode="3225130"
android:versionName="your.version" >
And the value of "3225130" is taken from the google-play-services_lib that your project is using. Also, that value is residing in the same location of manifest.xml in google-play-services_lib.
“3225130”的值取自您项目正在使用的google-play-services_lib。此外,该值位于google-play-services_lib中manifest.xml的相同位置。
Hope it will be of help.
希望它会有所帮助。
#7
1
If you look up Google Play Services in the Application manager it will show the version installed.
如果您在应用程序管理器中查找Google Play服务,则会显示已安装的版本。
#8
1
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();
}
}
#9
0
use the following function to check if google play services could be used or not
使用以下功能检查是否可以使用Google Play服务
private boolean checkGooglePlayServicesAvailable() {
final int connectionStatusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
return false;
}
return true;
}
#10
-2
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}
#1
80
I found simple solution:
我发现简单的解决方案
int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode;
#2
48
If you look in the link provided by Stan0 you will see this:
如果你查看Stan0提供的链接,你会看到:
public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE
Minimum Google Play services package version (declared in AndroidManifest.xml android:versionCode) in order to be compatible with this client version. Constant Value: 3225000 (0x003135a8)
最低Google Play服务包版本(在AndroidManifest.xml android:versionCode中声明),以便与此客户端版本兼容。常数值:3225000(0x003135a8)
So, when you set that in your manifest and then call isGooglePlayServicesAvailable(context)
:
因此,当您在清单中设置它然后调用isGooglePlayServicesAvailable(context)时:
public static int isGooglePlayServicesAvailable (Context context)
Verifies that Google Play services is installed and enabled on this device, and that the version installed on this device is no older than the one required by this client.
验证是否已在此设备上安装并启用了Google Play服务,并且此设备上安装的版本不早于此客户端所需的版本。
Returns
返回
- status code indicating whether there was an error. Can be one of following in ConnectionResult:
SUCCESS
,SERVICE_MISSING
,SERVICE_VERSION_UPDATE_REQUIRED
,SERVICE_DISABLED
,SERVICE_INVALID
.- 状态代码,指示是否有错误。可以是ConnectionResult中的以下之一:SUCCESS,SERVICE_MISSING,SERVICE_VERSION_UPDATE_REQUIRED,SERVICE_DISABLED,SERVICE_INVALID。
It will ensure that the device is using the version your app requires, if not, then you can take action according to the documentation
它将确保设备使用您的应用所需的版本,如果没有,那么您可以根据文档采取措施
Edit
GooglePlayServicesUtil.isGooglePlayServicesAvailable()
is deprecated and now GoogleApiAvailability.isGooglePlayServicesAvailable()
should be used instead.
不推荐使用GooglePlayServicesUtil.isGooglePlayServicesAvailable(),现在应该使用GoogleApiAvailability.isGooglePlayServicesAvailable()。
#3
13
here is my solution:
这是我的解决方案:
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;
}
}
and you have two options, either write your code directly in else block as commented, or use the returned boolean value for this method to write custom code.
并且您有两个选项,或者直接在else块中编写代码作为注释,或者使用返回的boolean值来编写自定义代码。
I hope this code helps someone.
我希望这段代码可以帮助别人。
#4
4
From newer updates i have ended up with this code, i have made a handy method to manage all stuff related to it..
从更新的更新我已经得到这个代码,我已经做了一个方便的方法来管理与它相关的所有东西..
All details related to availability of Service and related details are available here.
有关服务可用性和相关详细信息的所有详细信息,请参见此处。
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;
}
}
I use it like this,
我这样用,
GoogleApiAvailability avail;
int PLAY_SERVICE_STATUS = avail.isGooglePlayServicesAvailable(this);
checkPlayService(PLAY_SERVICE_STATUS);
And for version GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE;
will give you.
对于版本GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE;会给你。
And one of the most useful answer i found during my research is here.
我在研究期间找到的最有用的答案之一就是这里。
#5
2
IMPORTANT: GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE returns the minimum version required by YOUR app, not the play services version that is installed on the user's device.
重要提示:GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE会返回您应用所需的最低版本,而不是用户设备上安装的播放服务版本。
#6
1
I’ve run into the same problem this morning. And got it done by the advice from stan0. Thanks stan0.
今天早上我遇到了同样的问题。并且通过stan0的建议完成了它。谢谢stan0。
Only one change is needed based on the sample code from https://developers.google.com/maps/documentation/android/map.
根据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).
}
}
}
Besides, you need to add an attribute to your manifest.xml as:
此外,您需要将一个属性添加到manifest.xml中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name"
android:versionCode="3225130"
android:versionName="your.version" >
And the value of "3225130" is taken from the google-play-services_lib that your project is using. Also, that value is residing in the same location of manifest.xml in google-play-services_lib.
“3225130”的值取自您项目正在使用的google-play-services_lib。此外,该值位于google-play-services_lib中manifest.xml的相同位置。
Hope it will be of help.
希望它会有所帮助。
#7
1
If you look up Google Play Services in the Application manager it will show the version installed.
如果您在应用程序管理器中查找Google Play服务,则会显示已安装的版本。
#8
1
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();
}
}
#9
0
use the following function to check if google play services could be used or not
使用以下功能检查是否可以使用Google Play服务
private boolean checkGooglePlayServicesAvailable() {
final int connectionStatusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
return false;
}
return true;
}
#10
-2
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}