2017-07-10 4 views
0
package donet.mychatapp; 
import com.facebook.AccessToken; 
import com.facebook.CallbackManager; 
import com.facebook.FacebookCallback; 
import com.facebook.FacebookException; 
import com.facebook.FacebookSdk; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.content.pm.PackageInfo; 
import android.content.pm.PackageManager; 
import android.content.pm.Signature; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.v4.app.FragmentActivity; 
import android.support.v7.app.AppCompatActivity; 
import android.text.TextUtils; 
import android.util.Base64; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.facebook.login.LoginResult; 
import com.facebook.login.widget.LoginButton; 
import com.google.android.gms.auth.api.Auth; 
import com.google.android.gms.auth.api.signin.GoogleSignInAccount; 
import com.google.android.gms.auth.api.signin.GoogleSignInOptions; 
import com.google.android.gms.auth.api.signin.GoogleSignInResult; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.SignInButton; 
import com.google.android.gms.common.api.Api; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.PendingResult; 
import com.google.android.gms.common.api.Status; 
import com.google.android.gms.tasks.OnCompleteListener; 
import com.google.android.gms.tasks.Task; 
import com.google.firebase.auth.AuthCredential; 
import com.google.firebase.auth.AuthResult; 
import com.google.firebase.auth.FacebookAuthProvider; 
import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.auth.FirebaseAuth.AuthStateListener; 
import com.google.firebase.auth.FirebaseUser; 
import com.google.firebase.auth.GoogleAuthProvider; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 

import java.io.FileDescriptor; 
import java.io.PrintWriter; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
import java.util.HashMap; 
import java.util.concurrent.TimeUnit; 

public class StartActivity extends AppCompatActivity { 
    private SignInButton mGooglebtn; 
    private static final int RC_SIGN_IN = 1; 
    private GoogleApiClient mGoogleApiClient; 
    private FirebaseAuth mAuth; 
    private ProgressDialog progressDialog; 
    private static final String TAG2 = "FacebookLogin"; 
    private CallbackManager mCallbackManager; 
    private static final String TAG = "StartActivity"; 
    private LoginButton loginButton; 
    private FirebaseAuth.AuthStateListener mAuthListner; 
    private FirebaseUser mfirebaseuser; 
    private DatabaseReference databaseReference; 
TextView textView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_start); 
     mGooglebtn = (SignInButton) findViewById(R.id.mGooglebtn); 
     mAuth = FirebaseAuth.getInstance(); 
     progressDialog = new ProgressDialog(this); 
     textView=(TextView) findViewById(R.id.rough); 
     mAuthListner = new FirebaseAuth.AuthStateListener() { 

      @Override 
      public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
       if (firebaseAuth.getCurrentUser() != null) { 
        startActivity(new Intent(StartActivity.this, MainActivity.class)); 
       } 
      } 
     }; 

     loginButton = (LoginButton) findViewById(R.id.button_facebook_login); 
     mAuth = FirebaseAuth.getInstance(); 
// Initialize Facebook Login button 
     mCallbackManager = CallbackManager.Factory.create(); 



     loginButton.setReadPermissions("email", "public_profile"); 
     loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 
       Log.d(TAG2, "facebook:onSuccess:" + loginResult); 
       handleFacebookAccessToken(loginResult.getAccessToken()); 
      } 

      @Override 
      public void onCancel() { 
       Log.d(TAG2, "facebook:onCancel"); 
       // ... 
      } 

      @Override 
      public void onError(FacebookException error) { 
       Log.d(TAG2, "facebook:onError", error); 
       // ... 
      } 
     }); 


     // Configure Google Sign In 
     GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
       .requestIdToken(getString(R.string.default_web_client_id)) 
       .requestEmail() 
       .build(); 
     mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext()) 
       .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() { 

        @Override 
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
         Toast.makeText(StartActivity.this, "you got an error", Toast.LENGTH_LONG); 
        } 
       }) 
       .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
       .build(); 
     mGooglebtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       progressDialog.setMessage("please wait you are signing in"); 
       progressDialog.setCanceledOnTouchOutside(false); 
       progressDialog.show(); 

        // Name, email address, and profile photo Url 

        String name = mfirebaseuser.getDisplayName(); 
        String email = mfirebaseuser.getEmail(); 
        Uri photoUrl = mfirebaseuser.getPhotoUrl(); 
        String uid = mfirebaseuser.getUid(); 
        // register_user(name); 
        // textView.setText(email); 




       signIn(); 

      } 
     }); 


    } 


    @Override 
    protected void onStart() { 
     super.onStart(); 
     mAuth.addAuthStateListener(mAuthListner); 
     // Check if user is signed in (non-null) and update UI accordingly. 
     FirebaseUser currentUser = mAuth.getCurrentUser(); 


    } 

    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); 

     // Pass the activity result back to the Facebook SDK 
     mCallbackManager.onActivityResult(requestCode, resultCode, data); 
     // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); 
     if (requestCode == RC_SIGN_IN) { 
      GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
      GoogleSignInAccount acct = result.getSignInAccount(); 
      String personEmail = acct.getEmail(); 

      textView.setText(personEmail); 



      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 acct) { 
     Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId()); 

     AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); 
     mAuth.signInWithCredential(credential) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         if (task.isSuccessful()) { 
          // Sign in success, update UI with the signed-in user's information 
          Log.d(TAG, "signInWithCredential:success"); 
          FirebaseUser user = mAuth.getCurrentUser(); 
          // updateUI(user); 
         } else { 
          // If sign in fails, display a message to the user. 
          Log.w(TAG, "signInWithCredential:failure", task.getException()); 
          Toast.makeText(StartActivity.this, "Authentication failed.", 
            Toast.LENGTH_SHORT).show(); 
          //updateUI(null); 
         } 

         // ... 
        } 
       }); 
    } 


    public void needaccount(View view) { 
     Intent startintent = new Intent(StartActivity.this, RegisterActivity.class); 
     startActivity(startintent); 
     finish(); 
    } 

    public void alreadyaccount(View view) { 
     Intent intent = new Intent(this, signinactivity.class); 
     startActivity(intent); 
     finish(); 
    } 

    private void handleFacebookAccessToken(AccessToken token) { 
     Log.d(TAG, "handleFacebookAccessToken:" + token); 

     AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken()); 
     mAuth.signInWithCredential(credential) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         if (task.isSuccessful()) { 
          // Sign in success, update UI with the signed-in user's information 
          Log.d(TAG, "signInWithCredential:success"); 
          FirebaseUser user = mAuth.getCurrentUser(); 

         } else { 
          // If sign in fails, display a message to the user. 
          Log.w(TAG, "signInWithCredential:failure", task.getException()); 
          Toast.makeText(StartActivity.this, "Authentication failed.", 
            Toast.LENGTH_SHORT).show(); 

         } 


        } 
       }); 



     } 

    private void register_user(String name) { 


     String uid = mfirebaseuser.getUid(); 
     databaseReference = FirebaseDatabase.getInstance().getReference().child("users").child(uid); 
     HashMap<String, String> userMap = new HashMap<String, String>(); 
     userMap.put("name", name); 
     userMap.put("status", "Hi there I'm using Diet App"); 
     userMap.put("image", "default"); 
     userMap.put("thumb_image", "default"); 

     databaseReference.setValue(userMap); 
    } 

    } 
  1. Sag mir, wie Benutzernamen und grundlegende Informationen in Google anmelden und Facebook melden Sie sich mit Feuerbasis in Android zu drucken.
  2. Alle Methoden wie getdisplayname und get email id funktionieren nicht.
  3. Bitte senden Sie ein Beispiel oder Code, der mir helfen kann.
+0

„l Methoden wie GetDisplayName und E-Mail-ID erhalten nicht funktionieren“ Was genau meinen Sie nicht? Zurückkehrende Nullen? –

+0

Willkommen bei Stack Overflow! Du scheinst jemanden zu bitten, etwas Code für dich zu schreiben. Stack Overflow ist eine Frage-Antwort-Website, kein Code-Schreibdienst. Bitte [siehe hier] (http://stackoverflow.com/help/how-to-ask) um zu erfahren, wie man effektive Fragen schreibt. –

+0

@Rohan Starc, wenn ich Zeichenfolgen mit getdisplayname auf Textansicht drucken kein Name gedruckt –

Antwort

0

prüfen ein

providerspezifischen Profilinformationen Abschnitt des Benutzers Get

https://firebase.google.com/docs/auth/android/manage-users

+0

alles versucht, aber nicht funktioniert bro oder möglicherweise ich verwende es nicht richtig. Bitte senden Sie mir einen Beispielcode, wenn Sie haben. –

+0

Würden Sie das Code-Snippet mit dem Code teilen, der nicht funktioniert? –

Verwandte Themen