iT邦幫忙

1

Android studio Google登入問題

  • 分享至 

  • xImage

public class google_login extends AppCompatActivity {

private GoogleSignInClient mGoogleSignInClient;
private final static int RC_SIGN_IN = 123;
private FirebaseAuth mAuth;

@Override
protected void onStart() {
    super.onStart();

    FirebaseUser user = mAuth.getCurrentUser();
    if(user!=null){
        Intent intent = new Intent(getApplicationContext(),Profile.class);
        startActivity(intent);
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_google_login);

    mAuth = FirebaseAuth.getInstance();

    createRequest();

    SignInButton signInButton = findViewById(R.id.google_signIn);
    signInButton.setSize(SignInButton.SIZE_STANDARD);
    findViewById(R.id.google_signIn).setOnClickListener((View.OnClickListener) this);

}


private void createRequest() {

    // Configure Google Sign In
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    // Build a GoogleSignInClient with the options specified by gso.
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

}

private void signIn() {
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    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) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = task.getResult(ApiException.class);
            firebaseAuthWithGoogle(account);
        } catch (ApiException e) {
            // Google Sign In failed, update UI appropriately
            // ...
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {

    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
                        FirebaseUser user = mAuth.getCurrentUser();
                        Intent intent = new Intent(getApplicationContext(),Profile.class);
                        startActivity(intent);

                    } else {
                        Toast.makeText(google_login.this, "Sorry auth failed.", Toast.LENGTH_SHORT).show();

                    }

                    // ...
                }
            });
}

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.google_signIn:
            signIn();
            break;
        // ...
    }
}

}

我這是放在一個叫google_login.java的檔案裡面
因為MainActivity已經有寫入別的東西了
我想問為甚麼會無法執行
ps.第一次接觸android studio不知道要如何寫如果有錯誤請告知

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答