Java中写在手机端的账号的保存可以用session代替token来实现么密码已有方法去保存,怎么实现求大神帮忙
账号和密码可以使用 SharedPreferences 进行保存
这边已经保存了密码了,但是不能保存账号也用sharedpreferences来做保存么,具体代码是怎么实现的可以说一下么,我知道这个方法可以保存,但是没用过不知道怎么去操作,可以请教一下么
@解闷的浩宇: package com.zmsoft.TestTool.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import java.util.Map;
/**
* SharedPreferences工具类,用于写入和读取键值对
*/
public class SharedPreferencesUtil {
public static final String shareFile = "ELeadCommon";
public static final String user_id= "user_id";
public static final String bt="position";
public static final String shareFileInMulitProcess = "ELeadCommon_MulitProcess";
public static boolean getBoolean(Context context, String key) {
SharedPreferences settings = context.getSharedPreferences(shareFile,
Context.MODE_PRIVATE);
return settings.getBoolean(key, false);
}
public static boolean getBoolean(Context context, String key, boolean defaultBoolean) {
SharedPreferences settings = context.getSharedPreferences(shareFile,
Context.MODE_PRIVATE);
return settings.getBoolean(key, defaultBoolean);
}
public static boolean putBoolean(Context context, String key, boolean value) {
SharedPreferences settings = context.getSharedPreferences(shareFile,
Context.MODE_PRIVATE);
Editor edit = settings.edit();
edit.putBoolean(key, value);
return edit.commit();
}
/**
* 读取String类型值
*
* @param context
* @param key
* @return
*/
public static String getString(Context context, String key) {
SharedPreferences settings = context.getSharedPreferences(shareFile,
Context.MODE_PRIVATE);
return settings.getString(key, "");
}
public static boolean putString(Context context, String key, String value) {
if (context == null)
return false;
SharedPreferences settings = context.getSharedPreferences(shareFile,
Context.MODE_PRIVATE);
Editor edit = settings.edit();
edit.putString(key, value);
boolean commit = edit.commit();
return commit;
}
/**
* SharedPreferences 记录集一次性保存,提高存储效率
*
* @param context
* @param valueMap
* @return
*/
public static boolean putValueMap(Context context, Map<String, Object> valueMap) {
if (context == null)
return false;
SharedPreferences settings = context.getSharedPreferences(shareFile,
Context.MODE_PRIVATE);
Editor editor = settings.edit();
for (String key : valueMap.keySet()) {
Object value = valueMap.get(key);
if (value instanceof Boolean) {
editor.putBoolean(key, (Boolean) value);
} else if (value instanceof String) {
editor.putString(key, (String) value);
} else if (value instanceof Integer) {
editor.putInt(key, (Integer) value);
} else if (value instanceof Long) {
editor.putLong(key, (Long) value);
}
}
boolean commit = editor.commit();
//清空Map集合,避免重复存储数据信息
valueMap.clear();
return commit;
}
/**
* 读取String类型值, 不同进程同步的模式
*
* @param context
* @param key
* @return
*/
public static String getMulitString(Context context, String key) {
SharedPreferences settings = context.getSharedPreferences(
shareFileInMulitProcess, Context.MODE_MULTI_PROCESS);
return settings.getString(key, "");
}
public static boolean putMulitString(Context context, String key,
String value) {
SharedPreferences settings = context.getSharedPreferences(
shareFileInMulitProcess, Context.MODE_MULTI_PROCESS);
Editor edit = settings.edit();
edit.putString(key, value);
boolean commit = edit.commit();
return commit;
}
/**
* 读取Long类型值
*/
public static long getLong(Context context, String key) {
SharedPreferences settings = context.getSharedPreferences(shareFile,
Context.MODE_PRIVATE);
return settings.getLong(key, -1);
}
public static boolean putLong(Context context, String key, long value) {
SharedPreferences settings = context.getSharedPreferences(shareFile,
Context.MODE_PRIVATE);
Editor edit = settings.edit();
edit.putLong(key, value);
return edit.commit();
}
/**
* 读取Int类型值
*/
public static int getInt(Context context, String key) {
SharedPreferences settings = context.getSharedPreferences(shareFile,
Context.MODE_PRIVATE);
return settings.getInt(key, -1);
}
public static int getInt(Context context, String key, int defaultValue) {
SharedPreferences settings = context.getSharedPreferences(shareFile,
Context.MODE_PRIVATE);
return settings.getInt(key, defaultValue);
}
public static boolean putInt(Context context, String key, int value) {
SharedPreferences settings = context.getSharedPreferences(shareFile,
Context.MODE_PRIVATE);
Editor edit = settings.edit();
edit.putInt(key, value);
return edit.commit();
}
}
通过sharedpreferences工具类,键值对这是我的工具类,希望对你有所帮助
@monkey0928: 好的我去操作下,谢谢大神
@monkey0928: 它不需要去xml文件中配置账号的信息么,
@解闷的浩宇: 希望对你有所帮助,客气啦!!!
@解闷的浩宇: 是的
@monkey0928: xml文件中需要配置账号的信息么,大神,可以加下你扣扣么,以后小弟有什么不懂的情况在您不忙的时候,给予一个请教
@解闷的浩宇: 1370013083
@monkey0928: 谢了大神