SPutils,存取简单的字串,int,boolean及javabean对象
Posted dp_luo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SPutils,存取简单的字串,int,boolean及javabean对象相关的知识,希望对你有一定的参考价值。
可以结合Gson对javabean转化成字符串实现对象的存取,基本代码如下
package com.example.luozhenlonghp.project_aidl_demo;
import android.content.Context;
import android.content.SharedPreferences;
/**
* use for save data of
String,int,boolean,list<anybean>
* Created by luozhenlonghp on 2017/5/13.
*/
public class SPUtils {
/**
*
* @param context
* @param fileName
* @param key
* @param value
*/
public static void putString(Context context,String
fileName,String key,String value) {
SharedPreferences sp = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
SharedPreferences.Editor edit =
sp.edit();
edit.putString(key,value);
edit.commit();
}
/**
*
* @param context
* @param fileName
* @param key
* @return if
get failed return "";
*/
public static String getString(Context context,String fileName,String key) {
SharedPreferences sp =
context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
String result = sp.getString(key,
"");
return result;
}
public static void putInt(Context context,String fileName,String key,int value) {
SharedPreferences sp =
context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
SharedPreferences.Editor edit =
sp.edit();
edit.putInt(key,value);
edit.commit();
}
public static int getInt(Context context,String fileName,String key) {
SharedPreferences sp =
context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
int result = sp.getInt(key, 0);
return result;
}
public static void putBoolean(Context context,String fileName,String key,boolean value) {
SharedPreferences sp =
context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
SharedPreferences.Editor edit =
sp.edit();
edit.putBoolean(key,value);
edit.commit();
}
public static
boolean getBoolean(Context context,String fileName,String key) {
SharedPreferences sp =
context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
boolean result = sp.getBoolean(key, false);
return result;
}
}
以上是关于SPutils,存取简单的字串,int,boolean及javabean对象的主要内容,如果未能解决你的问题,请参考以下文章