我正在使用Facebook登录到我的应用程序。现在,我在我的项目LoginManager.getInstance()。registerCallback中没有遇到一个奇怪的问题。
我正在使用以下代码:
FacebookSdk.sdkInitialize(getActivity());//It showing as deprecated now
AppEventsLogger.activateApp(getActivity());//It showing as deprecated now
callbackManager = CallbackManager.Factory.create();
muvattapuzha
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
System.out.println("eeeee");
// App code
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code not called
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,birthday,gender");
request.setParameters(parameters);
request.executeAsync();
}
OnActivity结果方法:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
注意:我正在使用Activity-> fragment->片段。
Activity onResult方法,例如
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
for (Fragment fragment1 : fragment.getChildFragmentManager().getFragments()) {
fragment1.onActivityResult(requestCode, resultCode, data);
Log.d("Activity", "ON RESULT CALLED");
}
}
} catch (Exception e) {
Log.d("ERROR", e.toString());
}
}
这里可能是什么问题?
答案 0 :(得分:0)
使用此代码对我来说很好用。
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
callbackManager = CallbackManager.Factory.create();
FacebookSdk.sdkInitialize(LoginMainActivity.this);
setContentView(R.layout.activity_login_main);
loginButton = findViewById(R.id.activity_main_btn_login);
loginButton.setBackgroundResource(R.drawable.fb_rounded_broder);
loginButton.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
loginButton.setCompoundDrawablePadding(0);
loginButton.setText("");
loginButton.setReadPermissions(Arrays.asList(new String[]{"email", "user_birthday", "user_hometown"}));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "User login successfully");
getProfile();
}
@Override
public void onCancel() {
// App code
Log.d(TAG, "User cancel login");
}
@Override
public void onError(FacebookException exception) {
// App code
Log.d(TAG, "Problem for login");
}
});
public void getProfile() {
try {
accessToken = AccessToken.getCurrentAccessToken();
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.d(TAG, "Graph Object :" + object);
try {
String[] splitStr = object.getString("name").split("\\s+");
FBFirstname = splitStr[0];
FBLastName = splitStr[1];
FBEmail = object.getString("email");
FBUUID = object.getString("id");
Log.e(TAG, "firstnamev: " + splitStr[0]);
Log.e(TAG, "last name: " + splitStr[1]);
Log.e(TAG, "Email id : " + object.getString("email"));
Log.e(TAG, "ID :" + object.getString("id"));
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,birthday,gender,email");
request.setParameters(parameters);
request.executeAsync();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from
{
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}