首页 > 解决方案 > 如何避免 Firebase 由于 Play 服务版本错误而进入无限循环?

问题描述

我有一个基本的 Firebase 登录情况,代码如下:

    Log.d(TAG, "Entering black hole");
    try {
        FirebaseAuth.getInstance().signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(task -> {
                    Log.d(TAG, "Exiting black hole");
                    …
                }).addOnFailureListener(new OnFailureListener() {
            @Override public void onFailure(@NonNull Exception e) {
                Log.d(TAG, "Exiting black hole");
                …
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "try/catch " + e);
    }

但是,在装有 Android 7 的模拟器上运行它会生成以下日志:

D/FirebaseController: Entering black hole
W/GooglePlayServicesUtil: Google Play services out of date.  Requires 11925000 but found 11743470
D/EGL_emulation: eglMakeCurrent: 0x89b5a620: ver 2 0 (tinfo 0x89b5c680)
D/EGL_emulation: eglMakeCurrent: 0x89b5a620: ver 2 0 (tinfo 0x89b5c680)
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@415e769
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
W/GooglePlayServicesUtil: Google Play services out of date.  Requires 12451000 but found 11743470
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
W/GooglePlayServicesUtil: Google Play services out of date.  Requires 12451000 but found 11743470

意思是,FirebaseAuth 代码进入并且永远不存在,使我的应用程序陷入无限循环。.signInWithEmailAndPassword()我从日志中了解到 google play 服务已过时,但是在运行该方法之前如何检查呢?该isGooglePlayServicesAvailable()方法不允许传递版本号,似乎也没有返回任何版本号。

标签: androidfirebasefirebase-authentication

解决方案


int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode; 

这将返回播放服务版本,然后只需在 .signInWithEmailAndPassword() 之前检查它的程序

当您遇到 2 个 Google Play 服务 (GPS) 问题时,该问题是由特定情况引起的。由于 GPS 也被禁用了 Google Play Store (GPT),因此无法在设备上运行。

如果您的 Google Play 服务已过期,则使用 ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED 的错误代码(错误代码 2)调用 showErrorDialogFragment,这样可以正常工作。

但是,如果您的 GPS 已禁用且已过时,则 googleApiAvailability api 的工作方式存在问题。如果您调用 isGooglePlayServicesAvailable() 它将返回找到的第一个错误,但不一定是您想要的错误先解决。问题是知道您有另一个需要首先解决的错误。isGooglePlayServicesAvailable() 在这方面没有帮助。

在我的情况下,播放服务既被禁用,又过时。因此,该方法是首先调用 showErrorDialogFragment,您将获得 SERVICE_VERSION_UPDATE_REQUIRED 的响应错误代码。

Android 将尝试通过发送 pendingIntent 来启动 Google Play 商店 (GPT) 以更新 GPS 来解决此问题,但这会失败,因为 GPT 取决于 GPS 的启用版本。因为您正在调用 showErrorDialogFragment,所以它会在启动 GPT 失败后调用 onActivityResult。

下一步是编码 onActivityResult。我需要再次测试 isGooglePlayServicesAvailable() 。如果仍然得到相同的错误代码(SERVICE_VERSION_UPDATE_REQUIRED),那么您需要在 onActivityResult 中再次调用 showErrorDialogFragment,但这次传递一个不同的错误代码,ConnectionResult.SERVICE_DISABLED(错误代码 3)。这会将用户带到应用管理器以首先启用 google play 服务。然后当返回应用程序时,您需要测试 isGooglePlayServicesAvailable 并且它应该检测到谷歌服务仍然过期。如果您成功更新了应用程序,onActivityResult 应该允许您确定 isGooglePlayServicesAvailable 是否成功并且您可以继续。

(所以,googleApiAvailability 真正应该做的是首先返回禁用的错误(即 ConnectionResult.SERVICE_DISABLED 又名错误代码 3),因此您可以在尝试更新 GPS 之前先解决该问题。)


推荐阅读