我正在整合Facebook登錄在我的Android應用程序中,當我想要一個簡單的按鈕,然後點擊我想獲取用戶details.I不想放置Facebook登錄按鈕。我怎樣才能做到這一點如何獲取用戶的詳細信息上單擊按鈕不是在Facebook的按鈕在Android?
這裏是我的代碼: -
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
(){}
你的意思是你想的Facebook登錄一氣呵成工作嗎? – Moulesh
是................................................ ...... – Sandeep
如果你正在從Facebook獲取信息......並且不想顯示Facebook登錄信息,而是顯示一些不同的按鈕,那麼你的道德上是不正確的......而Facebook不會讓你獲取信息,除非用戶同意允許你的應用程序訪問他們的基本信息...至少用戶會知道信息正從fb中獲取。因此,單擊並獲取配置文件信息是不可能的。 – Moulesh