简体   繁体   English

Android登录按钮中的Facebook登录集成在登录后更改为注销

[英]Facebook login intergration in android login button changes to logout after logged in

I made facebook login integration using LoginButton in my android app.我在我的 android 应用程序中使用 LoginButton 进行了 facebook 登录集成。 now when i am logged in then my login button automatically changes to logout.现在,当我登录时,我的登录按钮会自动更改为注销。 I dont want that as i got another activity to logout.我不希望那样,因为我有另一个活动要注销。 how should i prevent this.我应该如何防止这种情况。 Following is my code.以下是我的代码。

public class FacebookLogin extends AppCompatActivity {

CallbackManager callbackManager;
RelativeLayout relativeLayout;
Bundle bundle;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    facebookSDKInitialize();
    setContentView(R.layout.activity_facebook_login);
    relativeLayout =(RelativeLayout)findViewById(R.id.relative_layout);

    bundle = new Bundle();

    if (AppStatus.getInstance(this).isOnline()) {

        LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setTextSize(16);
        loginButton.setReadPermissions("email");
        getLoginDetails(loginButton);



    } else {


        Snackbar snackbar = Snackbar
                .make(relativeLayout, "No internet connection!", Snackbar.LENGTH_INDEFINITE)
                .setAction("RETRY", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(FacebookLogin.this, FacebookLogin.class);
                        startActivity(intent);

                    }
                });

        // Changing message text color
        snackbar.setActionTextColor(Color.RED);

        // Changing action button text color
        View sbView = snackbar.getView();
        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.YELLOW);
        snackbar.show();
    }



}

/*
Initialize the facebook sdk.
And then callback manager will handle the login responses.
 */
protected void facebookSDKInitialize() {

    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
}

/*
Register a callback function with LoginButton to respond to the login result.
*/

protected void getLoginDetails(LoginButton login_button){

    // Callback registration
    login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult login_result) {
            getUserInfo(login_result);

        }

        @Override
        public void onCancel() {
            // code for cancellation
        }

        @Override
        public void onError(FacebookException exception) {
            //  code to handle error
        }
    });
}

/*
To get the facebook user's own profile information via  creating a new request.
When the request is completed, a callback is called to handle the success condition.
*/
protected void getUserInfo(LoginResult login_result){

    GraphRequest data_request = GraphRequest.newMeRequest(
            login_result.getAccessToken(),
            new GraphRequest.GraphJSONObjectCallback(){
                @Override
                public void onCompleted(
                        JSONObject json_object,
                        GraphResponse response) {

                    try {
                        final String name =json_object.getString("name");
                        final String email =json_object.getString("email");
                        bundle.putString("userName",name);
                        bundle.putString("userEmail",email);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    Intent intent = new Intent(FacebookLogin.this,HomePage.class);
                    intent.putExtras(bundle);
                    intent.putExtra("jsondata",json_object.toString());
                    startActivity(intent);
                    finish();
                }
            });
    Bundle permission_param = new Bundle();
    permission_param.putString("fields", "id,name,email,picture.width(120).height(120)");
    data_request.setParameters(permission_param);
    data_request.executeAsync();

}

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

    Log.e("data", data.toString());
}

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

    // Logs 'install' and 'app activate' App Events.
    AppEventsLogger.activateApp(this);
}

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

    // Logs 'app deactivate' App Event.
    AppEventsLogger.deactivateApp(this);
}
}

There is a very simple solution for that, just hide the facebook login button by setting its visibility to gone in the xml有一个非常简单的解决方案,只需通过在 xml 中将其visibility设置为gone来隐藏 facebook 登录按钮

then make your own button and setOnclickListener on it then inside your button, I am supposing your facebook button name is btnFacebook so write this inside of onclicklistener of your button btnFacebook.performClick();然后制作你自己的按钮并在其上setOnclickListener然后在你的按钮内,我假设你的Facebook按钮名称是btnFacebook所以在你的按钮btnFacebook.performClick();

and don't forget to use this in your logout button并且不要忘记在注销按钮中使用它

LoginManager.getInstance().logOut();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM