Google Api可用性问题

时间:2018-10-09 19:39:53

标签: android api maps

我正在尝试在我的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服务正在运行”的日志,但随后它就消失了,并返回false

Google Play服务已正确安装在我的SDK中。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

尝试此操作以获取布尔值并打印

    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(活动)