gpt4 book ai didi

java - 在类里面取消设置 session android

转载 作者:行者123 更新时间:2023-12-02 01:54:58 25 4
gpt4 key购买 nike

我有一个文件类,名称为Session.java然后我想创建销毁 session 的方法如何让它销毁方法内的 session ?

public class Session {
private SharedPreferences prefs;

public Session(Context cntx) {
// TODO Auto-generated constructor stub
prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
}

public String setusename(String userid) {
prefs.edit().putString("userid", userid).commit();
return userid;
}

public String getusename() {
String userid = prefs.getString("userid","");
return userid;
}


}

感谢您的关注

最佳答案

我希望这对你有用。

public class Session {

// Declare variable.

Context context;
SharedPreferences.Editor editor;
public static String PREF_NAME = "My_Pref";
public static int PREF_MODE = 0;
SharedPreferences prefs;

public Session(Context context) {
this.context = context;
prefs = context.getSharedPreferences(PREF_NAME, PREF_MODE);
editor = prefs.edit();
}

public String setusename(String userid) {
editor.putString("userid", userid);
editor.commit();
return userid;
}

public String getusename() {
return prefs.getString("userid",null);
}

//Method for destroy session given below.

public void destroySession() {
editor.clear();
editor.commit();
}
}

像这样在Activity中调用这个方法,

 Session session= new Session(getContext());

//用于设置UseId

session.setusename("your id");

//用于销毁 session

  session.destroySession();

关于java - 在类里面取消设置 session android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52436444/

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