【问题标题】:Google Api Availability issueGoogle API 可用性问题
【发布时间】:2018-10-09 19:39:52
【问题描述】:

我正在尝试在我的 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并返回false

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

【问题讨论】:

  • 它返回false,因为你已经写了return false,而且你永远不会返回任何其他东西。

标签: android api maps


【解决方案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,那么上面的代码就可以正常工作。

【讨论】:

  • 对不起,我忘了在 if 中复制返回。我要更新问题
  • @gabboSonc :: 除了"isServicesOk: Google Play Services is working 之外没有其他信息写入日志? Toast 是否显示?
  • 吐司已显示。在返回 true 之前,程序会跳出 if 并返回 false。
  • @gabboSonc :: 看看我对代码所做的细微修改。确实没有必要,但我添加了一个本地布尔变量isAvailable。看看这是否有效。我们可以在下一步中查看其余部分。
  • 听起来不错。现在它返回 true。在我用 if (checkGoogleServices()==true) 测试过的主要功能中,它工作正常。
【解决方案2】:

试试这个来获取布尔值并打印

    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(activity)

【讨论】:

    猜你喜欢
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 2023-03-31
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多