从firebase android

时间:2016-11-02 18:34:40

标签: android firebase firebase-authentication google-api-client

Welcome.java活动

public class Welcome extends AppCompatActivity {

private SignInButton signInButton;
private static final int RC_SIGN_IN = 1;
private static final String TAG = "Welcome";

private GoogleApiClient mGoogleApiClient;
private FirebaseAuth mAuth ;
private FirebaseAuth.AuthStateListener mAuthListener;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    //onClickNew_user();
    // Configure Google Sign In
    mAuth = FirebaseAuth.getInstance();

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

            if(firebaseAuth.getCurrentUser() != null)
            {

                Intent intent = new Intent("com.cablocator.my.cablocator.SetupDriver");
                startActivity(intent);
            }

        }
    };
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();



    GoogleApiClient  mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
            .enableAutoManage(this /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

                    Toast.makeText(Welcome.this, "some error occurred", Toast.LENGTH_SHORT).show();
                }
            })
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();


    signInButton = (SignInButton) findViewById(R.id.sign_in_button);
    signInButton.setSize(SignInButton.SIZE_STANDARD);
    signInButton.setScopes(gso.getScopeArray());

    signInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            signIn();
        }
    });
}

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

private void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = result.getSignInAccount();
            firebaseAuthWithGoogle(account);
        } else {
            // Google Sign In failed, update UI appropriately
            // ...
        }
    }
}

private void firebaseAuthWithGoogle(GoogleSignInAccount account) {

    AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(Welcome.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }
                    // ...
                }
            });
}


}

我希望获得有关SetupDriver活动的登录用户的详细信息,并将其存储在firebase数据库中,任何人都可以告诉我该怎么做。我在SetupDriver活动中的当前代码是:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setup_driver);

    mAuth = FirebaseAuth.getInstance();
    mDatabase = FirebaseDatabase.getInstance().getReference();



    onClickNextDriver();
  onClickSignOut();

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

            if(firebaseAuth.getCurrentUser() == null) {
                Intent intent = new Intent(SetupDriver.this,Welcome.class);
                startActivity(intent);
            }
        }
    };
}

public void onClickNextDriver()
{
    test_et = (EditText) findViewById(R.id.test);
    driver_btn = (Button)findViewById(R.id.setup_driver_btn);
    driver_btn.setOnClickListener(

            new View.OnClickListener()
            {
                @Override
                public void onClick(View v) {

                    getDetails();
                    setDetails();
                    if(validate() && validateReg() && validateLicense())
                    {
                        //test_et.setText(license_no);
                        mAuthListener = new FirebaseAuth.AuthStateListener() {
                            @Override
                            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

                                FirebaseUser user = firebaseAuth.getCurrentUser();

                                if(user != null) {
                                    String name = user.getDisplayName();
                                    String email = user.getEmail();
                                    String uid = user.getUid();
                                    mDatabase.child("name").setValue(name);

                                }

                            }
                        };

                        Toast.makeText(SetupDriver.this, "success", Toast.LENGTH_SHORT).show();

                    }
                    else {
                        Toast.makeText(SetupDriver.this, "please fill all the details", Toast.LENGTH_SHORT).show();
                    }
                }
            }
    );
}

logcat

11-06 15:16:33.529 29985-29985/com.cablocator.my.cablocator W/System: 


 ClassLoader referenced unknown path:  
/data/app/com.cablocator.my.cablocator-1/lib/arm

11-06 15:16:33.848 29985-29985/com.cablocator.my.cablocator W/System:  
ClassLoader referenced unknown path: /data/app/com.cablocator.my.cablocator-  
1/lib/arm
11-06 15:16:34.125 29985-30008/com.cablocator.my.cablocator   
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth   
not found.

11-06 15:16:34.139 29985-30008/com.cablocator.my.cablocator   
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth   
not found.
11-06 15:16:34.385 29985-29985/com.cablocator.my.cablocator W/art:  
BeforeAndroid 4.1,   

11-06 15:16:34.622 29985-29996/com.cablocator.my.cablocator W/art:  
Suspending all threads took: 10.139ms

 11-06 15:16:34.711 29985-29985/com.cablocator.my.cablocator W/System:    
ClassLoader referenced unknown path:    
/data/user/0/com.google.android.gms/app_chimera/m/0000000d/n/armeabi-v7a

11-06 15:16:34.711 29985-29985/com.cablocator.my.cablocator W/System:   
ClassLoader referenced unknown path:   
/data/user/0/com.google.android.gms/app_chimera/m/0000000d/n/armeabi

11-06 15:16:42.840 29985-29985/com.cablocator.my.cablocator W/System:   
ClassLoader referenced unknown path:  

/data/user/0/com.google.android.gms/app_chimera/m/0000000e/n/armeabi-v7a

11-06 15:16:42.841 29985-29985/com.cablocator.my.cablocator W/System:   
ClassLoader referenced unknown path: 


/data/user/0/com.google.android.gms/app_chimera/m/0000000e/n/armeabi

11-06 15:16:42.999 29985-29985/com.cablocator.my.cablocator  
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
11-06 15:16:42.999 29985-29985/com.cablocator.my.cablocator  
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
11-06 15:16:47.875 29985-29985/com.cablocator.my.cablocator 
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
11-06 15:16:47.875 29985-29985/com.cablocator.my.cablocator 
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
11-06 15:16:50.713 29985-30008/com.cablocator.my.cablocator 
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth 
not found.
11-06 15:16:56.602 29985-29985/com.cablocator.my.cablocator 
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
11-06 15:16:56.602 29985-29985/com.cablocator.my.cablocator  
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero       
length
11-06 15:16:56.605 29985-29985/com.cablocator.my.cablocator  
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero  
length
11-06 15:16:56.605 29985-29985/com.cablocator.my.cablocator  
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero  
length
11-06 15:17:11.210 29985-29991/com.cablocator.my.cablocator W/art:     
Suspending all threads took: 7.778ms

1 个答案:

答案 0 :(得分:1)

您对“ mGoogleClient从未使用”的评论指出了我的解决方案。改变这个:

GoogleApiClient  mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())...

到这个

mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())...

您正在创建GoogleApiClient的第二个实例,而不是定义signin()中使用的类成员。