从第3个活动获取MainActivity上的Facebook用户名

时间:2016-02-20 19:45:53

标签: android facebook android-intent

我有3个活动。 MainActivityside_nav_menu(后来我想要圆圈图片)。 FacebookActivityfacebook SDK和片段。 BlankFragment使用txtView获取当前用户名的所有facebook登录信息。

获取用户名,图片和电子邮件的最佳方法是什么。所以我可以在side_nav_menu上显示它。

callbackManager如何运作?

FacebookActivity扩展到Fragment

private BlankFragment mainFragment;
private LoginButton loginButton;
private CallbackManager callbackManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_facebook);



    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();


    loginButton = (LoginButton) findViewById(R.id.login_button);

    printHashkey();

    if (savedInstanceState == null) {
        // Add the fragment on initial activity setup
        mainFragment = new BlankFragment();
        getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, mainFragment).commit();
    } else {
        // Or set the fragment from restored state info
        mainFragment = (BlankFragment) getSupportFragmentManager()
                .findFragmentById(android.R.id.content);
    }



}




public void printHashkey(){
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "BLA BLA BLA BLA BLA BLA",
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
}

和碎片

private CallbackManager callbackManager;
private TextView textView;
private ImageView imv;

private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;

private FacebookCallback<LoginResult> callback = new  FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        Profile profile = Profile.getCurrentProfile();
        displayMessage(profile);


    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException e) {

    }
};

public BlankFragment() {

}


@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);


    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());

    callbackManager = CallbackManager.Factory.create();

    accessTokenTracker= new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {

        }
    };

    profileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
            displayMessage(newProfile);
        }
    };

    accessTokenTracker.startTracking();
    profileTracker.startTracking();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_blank, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
    textView = (TextView) view.findViewById(R.id.textView);

    loginButton.setReadPermissions("public_profile");
    loginButton.setFragment(this);
    loginButton.registerCallback(callbackManager, callback);

}

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

}

private void displayMessage(Profile profile){

    TextView textView = (TextView) getActivity().findViewById(R.id.textView);
    ProfilePictureView profileImage = (ProfilePictureView) getActivity().findViewById(R.id.profilePicture);


    if(profile != null){
        textView.setText(profile.getName());
        profileImage.setProfileId(profile.getId());
    }


}



@Override
public void onStop() {
    super.onStop();
    accessTokenTracker.stopTracking();
    profileTracker.stopTracking();
}

@Override
public void onResume() {
    super.onResume();
    Profile profile = Profile.getCurrentProfile();
    displayMessage(profile);



}

如何获取用户名和图片。发送到side_nav_menu上的MainActivity?

这部分显示了片段中的图片和名称。

private void displayMessage(Profile profile){

    TextView textView = (TextView) getActivity().findViewById(R.id.textView);
    ProfilePictureView profileImage = (ProfilePictureView) getActivity().findViewById(R.id.profilePicture);
    profileImage.setProfileId(profile.getId());

    if(profile != null){
        textView.setText(profile.getName());
        profileImage.setProfileId(profile.getId());

    }

}

1 个答案:

答案 0 :(得分:0)

您应该将其保存在SharedPreferences中(对于图像只保存路径,以便您可以从SD卡加载它,或者只保存图像URL)