android - Google API 可用性问题
问题描述
我正在尝试在我的 android 项目中使用 Google Maps API。当我调用以下函数时
public boolean checkGoogleServices(){
Log.d(TAG, "isServicesOk: checking google services version");
int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(Home.this);
if (available == ConnectionResult.SUCCESS){
Log.d(TAG, "isServicesOk: Google Play Services is working");
Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();
return true;
}
else if(GoogleApiAvailability.getInstance().isUserResolvableError(available)){
Log.d(TAG, "isServicesOk: an error occured but we can fix it");
Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(Home.this, available, ERROR_DIALOG_REQUEST);
dialog.show();
}else{
Toast.makeText(this, "You can't make maps request", Toast.LENGTH_LONG).show();
}
return false;
}
结果实际上if (available == ConnectionResult.SUCCESS)
是true
程序在控制台上写了日志“google play services is working”,但之后它就退出了 if 并 return false
。
Google Play 服务已正确安装在我的 SDK 中。
解决方案
该方法返回false
是因为您已经编写return false
了并且您在任何时候都不会返回任何其他内容。
true
如果您希望在服务可用时返回,请return
在条件中添加一条语句。
public boolean checkGoogleServices(){
boolean isAvailable = false;
Log.d(TAG, "isServicesOk: checking google services version");
int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(Home.this);
if (available == ConnectionResult.SUCCESS){
isAvailable = true;
Log.d(TAG, "isServicesOk: Google Play Services is working");
Toast.makeText(Home.this, "OK", Toast.LENGTH_LONG).show();
}
else if(GoogleApiAvailability.getInstance().isUserResolvableError(available)){
Log.d(TAG, "isServicesOk: an error occured but we can fix it");
Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(Home.this, available, ERROR_DIALOG_REQUEST);
dialog.show();
}else{
Toast.makeText(Home.this, "You can't make maps request", Toast.LENGTH_LONG).show();
}
return isAvailable;
}
该return
语句将退出该方法,以便不再执行任何代码。
您需要决定要在
}else if(GoogleApiAvailability.getInstance().isUserResolvableError(available)){
部分代码。如果你想返回false
,那么上面的代码可以正常工作。
推荐阅读
- angular - 无法在 Angular 7 中设置嵌入式报表的配置设置
- css - CSS Grid,有网格项的图像填充高度吗?
- c# - 在 sitefinity 版本 8.2 迁移后,页面重定向不保留 URL 的查询字符串部分?[asp.net/C#]
- java - 什么是实现以下问题的解决方案的最简单和最简洁的方法
- php - 根据当前登录用户获取表详细信息
- javascript - jquery fileupload.js this._on 不是函数错误
- php - Angular 模式表单 - 使用 PHP 和 Mysql 插入数据
- python - 使用 python/pandas 为 A 列中的每个唯一记录获取 B 列中的唯一值
- python - Scikit train_test_split 按索引
- sql - Oracle - 从 varchar2 列中提取用于比较的数字