如何在Android上单击而不是Facebook按钮的简单按钮上获取用户详细信息?

时间:2016-09-02 04:08:22

标签: android

我正在我的Android应用程序中集成facebook登录,当我点击一个简单的按钮时,我想要获取用户详细信息。我不想放置Facebook登录按钮。我怎么能这样做

这是我的代码: -

private Button loginButton;
private CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    setContentView(R.layout.activity_main);

    loginButton = (Button) findViewById(R.id.login_button);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile, email, user_birthday, user_friends"));
        }
    });
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(final LoginResult loginResult) {
                    Log.d("Success", "Login");
                    GraphRequest request = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject object, GraphResponse response) {
                                    Log.e("JSON:", object.toString());
                                    String user_ID = null;
                                    try {
                                        user_ID = object.getString("id");
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                    CFacbookStorage facebookstorage = new CFacbookStorage();
                                    facebookstorage.setM_UserId(user_ID);
                                    Intent i = new Intent(MainActivity.this, CFacebookDeatails.class);
                                    i.putExtra("user_data", facebookstorage);
                                    startActivity(i);
                                }
                            });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,email,gender, birthday");
                    request.setParameters(parameters);
                    request.executeAsync();

                }

                @Override
                public void onCancel() {
                    // Toast.makeText(Login.this, "Login Cancel", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onError(FacebookException exception) {
                    Toast.makeText(MainActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
    AccessToken token;
    token = AccessToken.getCurrentAccessToken();


    if (token == null) {
        //Means user is not logged in
    } else {
        Log.d("TAG", "init: facebooklogin   " + AccessToken.USER_ID_KEY + "   " + AccessToken.ACCESS_TOKEN_KEY + "  ");
    }
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile, email, user_birthday, user_friends"));

        }
    });
}


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

}

CFacbookStorage类代码: -

    public static CFacbookStorage cFacbookStorage;

public String getM_UserId() {
    return m_UserId;
}

public void setM_UserId(String m_UserId) {
    this.m_UserId = m_UserId;
}

public String m_UserId;

public static CFacbookStorage getInstance(){
    if (cFacbookStorage==null){
        cFacbookStorage = new CFacbookStorage();
    }
    return cFacbookStorage;
}

}

CFacebookDetail

private TextView info;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_details);
    info = (TextView)findViewById(R.id.info);

}

在这段代码中我在loginButton.registerCallback(){}

中遇到错误

2 个答案:

答案 0 :(得分:1)

在xml文件中使用了你的按钮,然后在使用下面代码的oncreate of activity:

/foo

在按钮点击事件上添加以下代码后:

private CallbackManager callbackManager;

callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(final LoginResult loginResult) {
                        Log.d("Success", "Login");
                        GraphRequest request = GraphRequest.newMeRequest(
                                loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(
                                            JSONObject object,
                                            GraphResponse response) {
                                        // Application code
                                        response.getError();
                                        Log.e("JSON:", object.toString());
                                    }
                                });
                        Bundle parameters = new Bundle();
                        parameters.putString("fields", "id,name,email,gender, birthday");
                        request.setParameters(parameters);
                        request.executeAsync();

                    }

                    @Override
                    public void onCancel() {
                        // Toast.makeText(Login.this, "Login Cancel", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        Toast.makeText(SignInActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });
        AccessToken token;
        token = AccessToken.getCurrentAccessToken();


        if (token == null) {
            //Means user is not logged in
        } else {
            Log.d("TAG", "init: facebooklogin   " + AccessToken.USER_ID_KEY + "   " + AccessToken.ACCESS_TOKEN_KEY + "  ");
        }

投入该活动

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    LoginManager.getInstance().logInWithReadPermissions(SignInActivity.this, Arrays.asList("public_profile, email, user_birthday, user_friends"));

            }
        });

答案 1 :(得分:0)

您可以使用FrameLayout覆盖您的appcompat按钮的facebook按钮视图来实现此目的。像这样:

           <FrameLayout
                android:id="@+id/FrameLayout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

            <com.facebook.login.widget.LoginButton
                android:id="@+id/facebook"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:background="@drawable/facebook"
                android:layout_gravity="center_horizontal"/>


            <ImageView
                android:id="@+id/fb_login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/facebook"
                android:onClick="onClick"
                android:layout_marginTop="10dp"
                android:layout_gravity="center_horizontal"/>

            </FrameLayout>

onClick实施:

 public void onClick(View v) {
        if (v == custom_fb_login) {
            fb_login.performClick();
        }
    }

FbButton点击:

 private View.OnClickListener handleClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
             case R.id.facebook:
                    LoginManager.getInstance().logInWithReadPermissions(Login.this, Arrays.asList("public_profile", "email"));
                    break;
                    }
                    }
                }