简体   繁体   中英

Android Custom Facebook Log In Button

I created custom button which works fine in my emulator. However, on the device it does not work: for the first time it logs in but at the second try it does perform any action.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_main);
    callbackManager = CallbackManager.Factory.create();
    Button testButton = (Button) findViewById(R.id.test);
    testButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            com.facebook.login.widget.LoginButton btn = new LoginButton(MainActivity.this);
            btn.setReadPermissions(Arrays.asList(
                    "public_profile", "email", "user_birthday", "user_friends"));

            if (Profile.getCurrentProfile().getCurrentProfile() != null) {
                LoginManager.getInstance().logOut();
                Button currentButton = (Button) findViewById(v.getId());
                currentButton.setText("LOG IN");
            } else {
                btn.performClick();
                Button currentButton = (Button) findViewById(v.getId());
                currentButton.setText("LOG OUT");
            }
        }
    });

    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {


                    GraphRequest.newMeRequest(
                            loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject json, GraphResponse response) {
                                    if (response.getError() != null) {
                                        // handle error
                                        Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();
                                    } else {

                                        try {

                                            user_name = json.getString("name");
                                            Toast.makeText(getApplicationContext(), user_name, Toast.LENGTH_LONG).show();
                                            System.out.println(user_name);
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                        Intent intent = new Intent(MainActivity.this, SignActivity.class);
                                        intent.putExtra(EXTRA_MESSAGE, user_name);
                                        startActivity(intent);
                                    }
                                }

                            }).executeAsync();

                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onError(FacebookException error) {

                }
            });

}


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

}

The application made public in facebook configuration page. Standard LoginButton works fine, however, I need to make it custom, or at least, to change button shape and color. Any ideas how to do that? Thank you in advance.

Facebook SDK version: 4.5 Emulator/device API versions: 23/21 (respectively)

The error was caused by newly generated key hash. Resolved the problem by adding new one (from stack trace) to the facebook application setttings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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