gpt4 book ai didi

android - 登录 : after going to another activity app forgot Facebook picture, 和名称文本

转载 作者:搜寻专家 更新时间:2023-11-01 08:43:27 25 4
gpt4 key购买 nike

我有两个问题无法解决,无法在我的 Android 应用程序中使用 Facebook SDK。

  1. (已解决)在进行另一项 Activity 后,或重新打开应用程序。它不记得个人资料图片和姓名(名字和姓氏)。

  2. 如果您选择注销,它仍会显示您的姓名和照片。

如果有人能帮助我,我会很高兴。请提供代码 - 不要说只是那样做。谢谢!

public class Profile extends Activity {
/**
* Called when the activity is first created.
*/

WebView web;
ArrayList<NavigationDrawerItem> listItems;
DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
ListView list;

private CallbackManager callbackManager;
private ProfilePictureView profilePictureView;
private TextView name;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_profile);
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile", "email", "user_friends");
final ProfilePictureView profilePictureView = (ProfilePictureView) findViewById(R.id.profilepic);
final TextView name = (TextView) findViewById(R.id.name);

final SharedPreferences sharedPrefs = getSharedPreferences("details", MODE_PRIVATE);
//After referencing your Views, add this.
String nameStr = sharedPrefs.getString("name", null);
String idStr = sharedPrefs.getString("id", null);
if(nameStr != null)
{
name.setText(nameStr);
}
if(idStr != null)
{
profilePictureView.setProfileId(idStr);
}
//.. Do the same for other profile data


loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
com.facebook.Profile profile = com.facebook.Profile.getCurrentProfile();
profilePictureView.getProfileId();
profilePictureView.setProfileId(profile.getId());
name.setText(profile.getName());

SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString("name", profile.getName());
editor.putString("id", profile.getId());
//.. Do the same for other profile data
editor.commit();

}



@Override
public void onCancel() {
// App code
}


@Override
public void onError(FacebookException exception) {
// App code
Log.i("Error", "Error");
}

});


}

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

最佳答案

将详细信息存储在 SharedPreferences 中并检索它们。

将此添加到您的 onCreate()

SharedPreferences sharedPrefs = getSharedPreferences("details", MODE_PRIVATE);

在回调的 onSuccess() 中添加,

Editor editor = sharedPrefs.edit();
editor.putString("name", profile.getName());
editor.putString("id", profile.getId());
//.. Do the same for other profile data
editor.commit();

再次在您的 onCreate() 中,

SharedPreferences sharedPrefs = getSharedPreferences("details", MODE_PRIVATE);
//After referencing your Views, add this.
String nameStr = sharedPrefs.getString("name", null);
String idStr = sharedPrefs.getString("id", null);
AccessToken token = AccessToken.getCurrentAccessToken();
if(token != null)
{
if(nameStr != null)
{
name.setText(nameStr);
}
if(idStr != null)
{
profilePictureView.setProfileId(id);
}
]
//.. Do the same for other profile data

关于android - 登录 : after going to another activity app forgot Facebook picture, 和名称文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30650068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com