OnActivityResult для Facebook и Google+ Войти в Android

Код API Facebook для входа в Android прост, с методом onActivityResult есть

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 callbackManager.onActivityResult(requestCode, resultCode, data);
}

В документации от Google говорится, что код для того же метода должен быть

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 Log.d(TAG, "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);
 if (requestCode == RC_SIGN_IN) {
 // If the error resolution was not successful we should not resolve further.
 if (resultCode != RESULT_OK) {
 mShouldResolve = false;
 }
 mIsResolving = false;
 mGoogleApiClient.connect();
 }
}

Предположим, что все переменные объявлены и инициализированы. Что должен сделать код для onActivityResult для обработки как входа в Facebook, так и G +.

Мой код входа (активности)

package cc.campusconnect.devapp;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.plus.Plus;
import org.json.JSONException;
import org.json.JSONObject;
public class Login extends AppCompatActivity implements
 GoogleApiClient.ConnectionCallbacks,
 GoogleApiClient.OnConnectionFailedListener,
 View.OnClickListener{
 private static final String TAG = "Login";
 LoginButton btnFBLogin;
 CallbackManager callbackManager;
 private static final int RC_SIGN_IN = 0;
 private GoogleApiClient mGoogleApiClient;
 /* Is there a ConnectionResult resolution in progress? */
 private boolean mIsResolving = false;
 /* Should we automatically resolve ConnectionResults when possible? */
 private boolean mShouldResolve = false;
 /* Request code used to invoke sign in user interactions. */
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 FacebookSdk.sdkInitialize(getApplicationContext());
 setContentView(R.layout.login);
 callbackManager = CallbackManager.Factory.create();
 btnFBLogin = (LoginButton) findViewById(R.id.fblogin);
 btnFBLogin.setReadPermissions("public_profile email");
 btnFBLogin.registerCallback(callbackManager, new FacebookCallback<loginresult>() {
 @Override
 public void onSuccess(LoginResult loginResult) {
 // App code
 if (AccessToken.getCurrentAccessToken() != null) {
 GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
 @Override
 public void onCompleted(JSONObject object, GraphResponse response) {
 JSONObject json = response.getJSONObject();
 try {
 if (json != null) {
 String text = "<b>Name :</b> " + json.getString("name") + "<b>Email :</b> " + json.getString("email") + "<b>Profile link :</b> " + json.getString("link");
 }
 } catch (JSONException e) {
 e.printStackTrace();
 }
 }
 });
 }
 }
 @Override
 public void onCancel() {
 // App code
 }
 @Override
 public void onError(FacebookException exception) {
 // App code
 }
 });
 mGoogleApiClient = new GoogleApiClient.Builder(this)
 .addConnectionCallbacks(this)
 .addOnConnectionFailedListener(this)
 .addApi(Plus.API)
 .addScope(new Scope(Scopes.PROFILE))
 .build();
 findViewById(R.id.sign_in_button).setOnClickListener(this);
 }
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 callbackManager.onActivityResult(requestCode, resultCode, data);
 }
 @Override
 public void onConnected(Bundle bundle) {
 }
 @Override
 public void onConnectionSuspended(int i) {
 }
 @Override
 public void onClick(View v) {
 if (v.getId() == R.id.sign_in_button) {
 onSignInClicked();
 }
 }
 private void onSignInClicked() {
 // User clicked the sign-in button, so begin the sign-in process and automatically
 // attempt to resolve any errors that occur.
 mShouldResolve = true;
 mGoogleApiClient.connect();
 // Show a message to the user that we are signing in.
 System.out.println("Signing in...");
 }
 @Override
 public void onConnectionFailed(ConnectionResult connectionResult) {
 // Could not connect to Google Play Services. The user needs to select an account,
 // grant permissions or resolve an error in order to sign in. Refer to the javadoc for
 // ConnectionResult to see possible error codes.
 Log.d(TAG, "onConnectionFailed:" + connectionResult);
 if (!mIsResolving && mShouldResolve) {
 if (connectionResult.hasResolution()) {
 try {
 connectionResult.startResolutionForResult(this, RC_SIGN_IN);
 mIsResolving = true;
 } catch (IntentSender.SendIntentException e) {
 Log.e(TAG, "Could not resolve ConnectionResult.", e);
 mIsResolving = false;
 mGoogleApiClient.connect();
 }
 } else {
 // Could not resolve the connection result, show the user an
 // error dialog.
 //showErrorDialog(connectionResult);
 System.out.println("Couldn't connect");
 }
 } else {
 // Show the signed-out UI
 //showSignedOutUI();
 System.out.println("Signed out");
 }
 }
 @Override
 protected void onStart() {
 super.onStart();
 mGoogleApiClient.connect();
 }
 @Override
 protected void onStop() {
 super.onStop();
 mGoogleApiClient.disconnect();
 }
}
</loginresult>
1 ответ

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 Log.d(TAG, "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);
if (requestCode == RC_SIGN_IN) {
 // If the error resolution was not successful we should not resolve further.
 if (resultCode != RESULT_OK) {
 mShouldResolve = false;
 }
 mIsResolving = false;
 mGoogleApiClient.connect();
}else{
 //If not request code is RC_SIGN_IN it must be facebook
 callbackManager.onActivityResult(requestCode, resultCode, data);
 }
}

licensed under cc by-sa 3.0 with attribution.