在我的应用程序中,我需要检查Google Play服务版本(已安装在用户设备中)。可能吗 ?如果是的话,我该怎么做?我搜索了Google,但找不到任何东西!
在我的应用程序中,我需要检查Google Play服务版本(已安装在用户设备中)。可能吗 ?如果是的话,我该怎么做?我搜索了Google,但找不到任何东西!
Answers:
我找到了简单的解决方案:
int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode;
但是versionCode
API 28已弃用,因此您可以使用PackageInfoCompat
:
long v = PackageInfoCompat.getLongVersionCode(getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ));
如果您在通过Stan0提供的链接看,你会看到这个:
public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE
最低Google Play服务软件包版本(在AndroidManifest.xml android:versionCode中声明),以便与此客户端版本兼容。常数值:3225000(0x003135a8)
因此,当您在清单中设置该名称然后调用isGooglePlayServicesAvailable(context)
:
public static int isGooglePlayServicesAvailable (Context context)
验证是否已在此设备上安装并启用了Google Play服务,并且此设备上安装的版本不早于该客户端所需的版本。
退货
- 状态代码,指示是否存在错误。可以按照ConnectionResult之一:
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块中编写代码,或者使用此方法返回的布尔值编写自定义代码。
我希望这段代码对某人有帮助。
从较新的更新中,我最终获得了此代码,我提出了一种方便的方法来管理与此相关的所有内容。
有关服务可用性的所有详细信息以及相关详细信息,请参见此处。
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();
}
}
今天早上我遇到了同样的问题。并根据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.xml中,如下所示:
<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的相同位置。
希望对您有所帮助。
更新(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
}
}
}
}
使用以下功能检查是否可以使用Google Play服务
private boolean checkGooglePlayServicesAvailable() {
final int connectionStatusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
return false;
}
return true;
}
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}