我在 Android 应用程序中使用 Facebook SDK 时遇到两个问题,无法解决。
(已解决)进入其他 Activity 后,或重新打开应用程序。它不记得个人资料图片和姓名(名字和姓氏)。
如果您选择注销,它仍会显示您的姓名和图片。
如果有人可以帮助我,我将非常高兴。请提供代码 - 不要说只是这样做。谢谢你!
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/