码迷,mamicode.com
首页 > 编程语言 > 详细

SPutils,存取简单的字串,int,boolean及javabean对象

时间:2017-05-13 23:25:34      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:mit   strong   use   put   har   fail   public   class   data   

可以结合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对象

标签:mit   strong   use   put   har   fail   public   class   data   

原文地址:http://www.cnblogs.com/dp-luo/p/6850463.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!