如何制作下面的代码来记住用户在 Android 应用程序中进行的登录。我需要应用程序记住一些数据,例如用户名和个人资料图片,但现在,当我重新启动(退出并重新打开)应用程序时,应用程序会丢失用户名和个人资料图片等数据
public class SelfTrail extends AppCompatActivity {
private LoginButton btnLogin;
private TextView facebookName;
private CallbackManager callbackManager;
private ProfilePictureView profilePictureView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_self_trail);
btnLogin = (LoginButton)findViewById(R.id.login_button);
facebookName = (TextView)findViewById(R.id.name);
profilePictureView = (ProfilePictureView)findViewById(R.id.image);
btnLogin.setReadPermissions(Arrays.asList("public_profile, email"));
callbackManager = CallbackManager.Factory.create();
btnLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("Main", response.toString());
setProfileToView(object);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
private void setProfileToView(JSONObject jsonObject) {
try {
facebookName.setText(jsonObject.getString("name"));
profilePictureView.setPresetSize(ProfilePictureView.NORMAL);
profilePictureView.setProfileId(jsonObject.getString("id"));
} catch (JSONException e) {
e.printStackTrace();
}
}}
请随时更正代码,提前致谢
最佳答案
我建议您将个人资料图片保存在本地,并记住该图片位置的字符串以及用户名和密码。这是我开始使用的一个简单的存储类,用于存储用户名和密码等信息
public class UserLocalStore {
public int x = 0;
public static final String SP_NAME = "userDetails";
SharedPreferences userLocalDatabase;
public int currentID;
public UserLocalStore(Context context){
userLocalDatabase = context.getSharedPreferences(SP_NAME, 0);
}
public void storeUserData(User user){
SharedPreferences.Editor spEditor = userLocalDatabase.edit();
//spEditor.putString("ID", Integer.toString(x));
spEditor.putString(Integer.toString(x) +" name", user.name);
spEditor.putString(Integer.toString(x) +" username", user.username);
spEditor.putString(Integer.toString(x) +" password", user.password);
x++;
spEditor.commit();
}
public int getByUsername(String findusername){
int x = 0;
while(x < 10){
String test = userLocalDatabase.getString(Integer.toString(x)+ " username", "");
if(test.equals(findusername)){
String name = userLocalDatabase.getString(Integer.toString(x)+" name", "");
String username = userLocalDatabase.getString(Integer.toString(x)+" username", "");
String password = userLocalDatabase.getString(Integer.toString(x)+" password", "");
User foundUser = new User(name,username,password);
return x;
}
x++;
}
return 0;
}
public ArrayList<User> getAllUsers(){
int x = 0;
ArrayList<User> allUsers = new ArrayList<>();
User tempuser = new User("","","");
while(x < 10) {
if(userLocalDatabase.getString(Integer.toString(x) + " name", "").equals("")) {
return allUsers;
}
String name = userLocalDatabase.getString(Integer.toString(x) + " name", "");
String username = userLocalDatabase.getString(Integer.toString(x) + " username", "");
String password = userLocalDatabase.getString(Integer.toString(x) + " password", "");
tempuser.name = name;
tempuser.username = username;
tempuser.password = password;
allUsers.add(tempuser);
x++;
} return allUsers;
}
public User getLoggedInUser(int ID){
String name = userLocalDatabase.getString(Integer.toString(ID)+" name", "");
String username = userLocalDatabase.getString(Integer.toString(ID)+" username", "");
String password = userLocalDatabase.getString(Integer.toString(ID)+" password", "");
User storedUser = new User(name,username,password);
return storedUser;
}
public User getStoredPreferences(){
String name = userLocalDatabase.getString("name", "");
String username = userLocalDatabase.getString("username", "");
String password = userLocalDatabase.getString("password", "");
User storedUser = new User(name,username,password);
return storedUser;
}
public void setUserLoggedIn(int loggedIn){
currentID = loggedIn;
}
public int getUserLoggedIn() {
return currentID;
}
public void clearUserData(){
SharedPreferences.Editor spEditor = userLocalDatabase.edit();
spEditor.clear();
spEditor.commit();
} }
然后你可以像这样从另一个类中调用它
User addthisuser = new User(name, username, password);
userLocalStore.storeUserData(addthisuser);
关于java - 使应用程序在退出应用程序后记住 Facebook 登录 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36372835/