首页 > 解决方案 > 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 中。

标签: androidapimaps

解决方案


该方法返回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,那么上面的代码可以正常工作。


推荐阅读