赞
踩
/**
* shared preference 字段名
* @author
*
*/
public class Config {
public static final String FILE = "config";
public static final String USER_ID = "user_id";
public static final String TOKEN = "token";
/**
* 退出登录时清理登录信息
* @param context
*/
public static void logout(Context context) {
SharedPreferences sharedPref = context.getSharedPreferences(Config.FILE, Context.MODE_PRIVATE);
Editor editor = sharedPref.edit();
editor.remove(Config.USER_ID);
editor.remove(Config.TOKEN);
editor.commit();
}
/**
* 是否已登录
* @param context
* @return
*/
public static boolean isLogined(Context context){
SharedPreferences sharedPref = context.getSharedPreferences(Config.FILE, Context.MODE_PRIVATE);
String userId = sharedPref.getString(Config.USER_ID, "");
if(!TextUtils.isEmpty(userId)){
return true;
}
return false;
}
public static String getUserId(Context context){
SharedPreferences sharedPref = context.getSharedPreferences(Config.FILE, Context.MODE_PRIVATE);
return sharedPref.getString(USER_ID, "");
}
public static void setUserId(Context context, String userId){
SharedPreferences sharedPref = context.getSharedPreferences(Config.FILE, Context.MODE_PRIVATE);
Editor editor = sharedPref.edit();
editor.putString(Config.USER_ID, userId);
editor.commit();
}
public static String getToken(Context context){
SharedPreferences sharedPref = context.getSharedPreferences(Config.FILE, Context.MODE_PRIVATE);
return sharedPref.getString(TOKEN, "");
}
public static void setToken(Context context, String token){
SharedPreferences sharedPref = context.getSharedPreferences(Config.FILE, Context.MODE_PRIVATE);
Editor editor = sharedPref.edit();
editor.putString(Config.TOKEN, token);
editor.commit();
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。