FIrebase AuthStateListener未被调用

时间:2017-08-24 14:22:54

标签: android firebase firebase-authentication firebaseui

我正在尝试使用firebase UI调用AuthStateListener以进行firebase用户登录,但是在执行活动时不会调用该方法。我之前为各种应用程序做了相同的过程以下是代码:

mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();

            if (firebaseUser != null) {
                Log.d(TAG, "In SignedinFirebaseMethod");

            } else {
                startActivityForResult(
                        AuthUI.getInstance()
                                .createSignInIntentBuilder()
                                .setIsSmartLockEnabled(false)
                                .setProviders(AuthUI.GOOGLE_PROVIDER,
                                        AuthUI.EMAIL_PROVIDER)
                                .build(),
                        RC_SIGN_IN);
            }
        }
    };

以下是Gradle代码:

android {
compileSdkVersion 25
buildToolsVersion "24.0.1"
defaultConfig {
    applicationId "com.example.hamid_pc.auctionapp"
    minSdkVersion 16
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
 buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.google.firebase:firebase-database:10.0.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'net.danlew:android.joda:2.9.9'
compile 'com.google.firebase:firebase-storage:10.0.1'
compile 'com.google.firebase:firebase-auth:10.0.1'
compile 'com.firebaseui:firebase-ui:1.1.1'
testCompile 'junit:junit:4.12'
 }

 apply plugin: 'com.google.gms.google-services'

2 个答案:

答案 0 :(得分:0)

尝试在使用firebase执行auth之前注册侦听器。您可能会延迟收听监听器。

abstract void onAuthStateChanged(FirebaseAuth auth)

此方法在UI线程中调用身份验证状态的更改:

  • 听众注册后

  • 当用户登录

  • 当前用户退出时当前用户更改

答案 1 :(得分:-1)

我发现代码中的错误我没有在启动片段时附加监听器

@Override
public void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}