Android中的Facebook和Google登录的onActivityResult代码

Android中的Facebook和Google登录的onActivityResult代码,第1张

概述用于在 Android中登录的Facebook API代码很简单,方法onActivityResult是 protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); 用于在 Android中登录的Facebook API代码很简单,方法onActivityResult是
protected voID onActivityResult(int requestCode,int resultCode,Intent data) {        super.onActivityResult(requestCode,resultCode,data);        callbackManager.onActivityResult(requestCode,data);}

Google的文档说明了相同方法的代码必须是

@OverrIDepublic voID onActivityResult(int requestCode,Intent data) {    super.onActivityResult(requestCode,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();    }}

假设声明并初始化所有变量.为了处理Facebook和G登录,onActivityResult的代码必须是什么.

我的登录(活动)代码是

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 in@R_502_6704@ctions. */    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") + "<br><br><b>Email :</b> " + Json.getString("email") + "<br><br><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,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.        Sy@R_419_6563@.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);                Sy@R_419_6563@.out.println("Couldn't connect");            }        } else {            // Show the signed-out UI            //showSignedOutUI();            Sy@R_419_6563@.out.println("Signed out");        }    }    @OverrIDe    protected voID onStart() {        super.onStart();        mGoogleapiclient.connect();    }    @OverrIDe    protected voID onStop() {        super.onStop();        mGoogleapiclient.disconnect();    }}
解决方法
@OverrIDepublic voID onActivityResult(int requestCode,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,data); }}
总结

以上是内存溢出为你收集整理的Android中的Facebook和Google登录的onActivityResult代码全部内容,希望文章能够帮你解决Android中的Facebook和Google登录的onActivityResult代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1143671.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存