脸书登录和谷歌+登录的请求代码



我两天前发布了这个问题。onActivityResult方法用于google+和Facebook

我想也许我可以创建两个请求代码,一个用于facebook,一个为google+,然后在onActivityResult方法中,我可以使用请求代码创建一个switchcase或else-if语句。但我不知道该怎么做。我如何才能获得facebook的登录意向。有人能帮我写那行代码吗?如果我的想法真的能奏效的话。。。

public abstract class google_abstract extends Activity {
CallbackManager callbackManager;
FirebaseAuth auth;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_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);
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("Success", "Login");
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Login Cancel", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
int RC_SIGN_IN_G = 0;
int RC_SIGN_IN_F = 0;
GoogleSignInClient mGoogleSignInClient;
public void signIn_g() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN_G);
}
**public void signIn_f(){
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN_F);
}**
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN_G) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
else if (requestCode == RC_SIGN_IN_F) {
callbackManager.onActivityResult(requestCode,resultCode,data );
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "public_profile"));
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
startActivity(new Intent(this, tags.class));
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w("Google Sign In Error", "signInResult:failed code=" + e.getStatusCode());
//            pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onStart() {
// Check for existing Google Sign In account, if the user is already signed in
// the GoogleSignInAccount will be non-null.
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (account != null) {
startActivity(new Intent(this, tags.class));
}
super.onStart();
}
private void handleFacebookToken(AccessToken accessToken)
{
AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
auth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
FirebaseUser myuserobj=auth.getCurrentUser();
updateUI(myuserobj);
}
else
{
Toast.makeText(getApplicationContext(),"could not register to firebase",Toast.LENGTH_SHORT).show();
}
}
});
}
private void updateUI(FirebaseUser myuserobj) {
String e_mail= myuserobj.getEmail();
}
}

当前存储的是相同的int(此处为0(,因此onActivityResult将始终位于第一个if语句中。解决方案-首先,你需要像这个一样全局取整数值

int RC_SIGN_IN_G = 0;

对于谷歌,你需要有请求代码,然后在onActivityResult中获取它并检查,对于fb,在超级调用之前包括这一行

callbackManager.onActivityResult(requestCode, resultCode, data);

并从onActivityResult中删除fb请求代码
要调用fb登录,您需要根据其id找到fb登录按钮,并附加回调侦听器

onActivityResult的最终代码在这里

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//include this too
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN_G) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}  

编辑添加的FB登录代码
对于谷歌,对FB保持不变使用此
FB登录代码

loginButton = (LoginButton) findViewById(R.id.login_button); 
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});

最新更新