我试图在我的android项目中使用Google。当我调用以下函数时
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服务正在工作“,但在那之后,它就会退出if并返回false。
Google服务已正确安装在我的SDK中。
发布于 2018-10-09 19:57:18
该方法返回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,那么上面的代码就可以正常工作了。
发布于 2018-10-09 20:02:04
尝试这个以获得布尔值并打印
public static boolean checkPlayServices(Activity activity) {
final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity);
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(resultCode)) {
apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
.show();
Log.d(TAG, "This device have google play services");
} else {
Log.d(TAG, "This device does not have google play services");
}
return false;
}
return true;
}现在打印checkPlayServices(活动)
https://stackoverflow.com/questions/52728218
复制相似问题