簡體   English   中英

Android自定義Facebook登錄按鈕

[英]Android Custom Facebook Log In Button

我創建了自定義按鈕,該按鈕在模擬器中可以正常工作。 但是,它在設備上不起作用:第一次登錄,但第二次嘗試執行任何操作。

@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);

}

該應用程序在Facebook配置頁面上公開。 標准LoginButton可以正常工作,但是,我需要對其進行自定義,或者至少要更改按鈕的形狀和顏色。 任何想法如何做到這一點? 先感謝您。

Facebook SDK版本:4.5仿真器/設備API版本:分別為23/21

該錯誤是由新生成的密鑰哈希值引起的。 通過向Facebook應用程序設置中添加新內容(來自堆棧跟蹤)解決了該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM