You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用Firebase连接异常:GMS核心API调用报错求助

问题解决:GoogleApiAvailability报错ConnectionResult=0

问题分析

ConnectionResult=0对应的是ConnectionResult.SUCCESS,代表Google Play服务本身是可用的。你报错出现在getErrorString(result)这一行,是因为这个方法仅针对非成功状态的错误码设计,当传入成功码(0)时,内部可能无法找到对应的错误字符串,从而触发异常。

另外你的代码逻辑存在问题:不管Google Play服务是否正常,都调用了showErrorNotification和getErrorString,这两个方法只应该在服务不可用的时候调用。

修正代码

调整逻辑,仅在非成功状态下处理错误:

int result = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
if (result != ConnectionResult.SUCCESS) {
    GoogleApiAvailability.getInstance().showErrorNotification(this, result);
    String msg = GoogleApiAvailability.getInstance().getErrorString(result);
    // 处理错误信息,比如Log输出或提示用户
    Log.e("GMS_ERROR", "Google Play服务错误: " + msg);
} else {
    // Google Play服务正常,执行Firebase相关操作
}

设备端排查步骤

如果调整代码后仍有问题,检查你的设备:

  • 打开Google Play商店,确保Google Play服务已更新到最新版本
  • 若使用模拟器,确认镜像包含Google Play服务(选择带「Google APIs」或「Google Play」标签的系统镜像)
  • 检查设备是否禁用了Google Play服务,或限制了其后台权限

内容的提问来源于stack exchange,提问作者Joe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 22:30:51