SharedPreferences工具类(2种)

SharedPreferences工具类(2种)_第1张图片

SharedPreferences工具类(2种)_第2张图片

SharedPreferences工具类(2种)_第3张图片


下载地址  点击打开链接

-----------------------------------------------------------------首先看方法1------------------------------------------------------------------------------

SharedPreferencesUtils1

package com.example.sharedpreferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;

public class SharedPreferencesUtils1 {

	private String name;

	private Context mContext;

	/**
	 * 当instance==null时候,加同步锁
	 * 
	 * @return
	 */
	private static SharedPreferencesUtils1 instance;

	public static SharedPreferencesUtils1 getInstance() {
		if (instance == null) {
			synchronized (SharedPreferencesUtils1.class) {
				if (instance == null) {
					instance = new SharedPreferencesUtils1();
				}
			}
		}
		return instance;
	}

	private SharedPreferencesUtils1() {
	}

	/**
	 * 初始化上下文参数以及文件名
	 * 
	 * @param context
	 *            ,上下文
	 * @param name
	 *            ,文件名
	 */
	public void init(Context context, String name) {
		this.mContext = context.getApplicationContext();
		this.name = name;
	}

	/**
	 * 保存数据,泛型方法
	 * 
	 * @param key
	 *            ,键值
	 * @param value
	 *            ,数据
	 * @param 
	 */
	public  void setValue(@NonNull String key, V value) {
		SharedPreferences sp = mContext.getSharedPreferences(name,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		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);
		} else if (value instanceof Boolean) {
			editor.putBoolean(key, (Boolean) value);
		} else if (value instanceof Float) {
			editor.putFloat(key, (Float) value);
		}
		editor.commit();
	}

	/**
	 * 读取数据,泛型方法
	 * 
	 * @param key
	 *            ,键值
	 * @param defaultValue
	 *            ,默认值
	 * @param 
	 * @return
	 */

	public  V getValue(@NonNull String key, V defaultValue) {
		SharedPreferences sp = mContext.getSharedPreferences(name,
				Context.MODE_PRIVATE);
		Object value = defaultValue;
		if (defaultValue instanceof String) {
			value = sp.getString(key, (String) defaultValue);
		} else if (defaultValue instanceof Integer) {
			value = sp.getInt(key, (Integer) defaultValue);
		} else if (defaultValue instanceof Long) {
			value = sp.getLong(key, (Long) defaultValue);
		} else if (defaultValue instanceof Boolean) {
			value = sp.getBoolean(key, (Boolean) defaultValue);
		} else if (defaultValue instanceof Float) {
			value = sp.getFloat(key, (Float) defaultValue);
		}
		return (V) value;
	}

	/**
	 * 清除数据
	 */
	public void clearData() {
		SharedPreferences.Editor editor = mContext.getSharedPreferences(name,
				Context.MODE_PRIVATE).edit();
		editor.clear();
		editor.commit();
	}
}

-----------------------------------------------------------------继续看方法2------------------------------------------------------------------------------

GDPreferenceSettings

package com.example.sharedpreferences;

public enum GDPreferenceSettings {

	SETTING_IS_FIRST("com.godinsec.glauncher_isFirstIn", Boolean.TRUE);
	private final String mId;
	private final Object mDefaultValue;

	private GDPreferenceSettings(String id, Object defaultValue) {
		this.mId = id;
		this.mDefaultValue = defaultValue;
	}

	public String getId() {
		return this.mId;
	}

	public Object getDefaultValue() {
		return this.mDefaultValue;
	}

}

GDPreferences

package com.example.sharedpreferences;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class GDPreferences {

	public static final String CCP_PREFERENCE = "geek2.xml";

	private static GDPreferences instance;

	public static GDPreferences getInstance() {
		if (instance == null) {
			synchronized (GDPreferences.class) {
				if (instance == null) {
					instance = new GDPreferences();
				}
			}
		}
		return instance;
	}

	private GDPreferences() {
		super();
	}


	public static SharedPreferences getSharedPreferences() {
		return MyApplication.getInstance().getSharedPreferences(CCP_PREFERENCE,
				Context.MODE_PRIVATE);
	}

	public static void savePreference(GDPreferenceSettings GDPreferenceSetting,
			Object value) {
		Map prefs = new HashMap();
		prefs.put(GDPreferenceSetting, value);
		savePreferences(prefs);
	}

	private static void savePreferences(Map prefs) {
		SharedPreferences sp = getSharedPreferences();
		Editor editor = sp.edit();

		Iterator it = prefs.keySet().iterator();
		while (it.hasNext()) {
			GDPreferenceSettings pref = it.next();

			Object value = prefs.get(pref);
			if (value == null) {
				return;
			}
			if (value instanceof Boolean
					&& pref.getDefaultValue() instanceof Boolean) {
				editor.putBoolean(pref.getId(),
						((Boolean) value).booleanValue());
			} else if (value instanceof String
					&& pref.getDefaultValue() instanceof String) {
				editor.putString(pref.getId(), (String) value);
			} else if (value instanceof Integer
					&& pref.getDefaultValue() instanceof Integer) {
				editor.putInt(pref.getId(), (Integer) value);
			} else if (value instanceof Long
					&& pref.getDefaultValue() instanceof Long) {
				editor.putLong(pref.getId(), (Long) value);
			}

		}
		editor.commit();

	}
}

MyApplication

package com.example.sharedpreferences;

import android.app.Application;

public class MyApplication extends Application {
	private static MyApplication instance;
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		this.instance = this;
	}

	public static MyApplication getInstance(){
		return instance;
	}

}

最后看测试代码

MainActivity

package com.example.sharedpreferences;

import android.app.Activity;
import android.os.Bundle;
import android.text.StaticLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

	private static final String TAG = MainActivity.class.getSimpleName();
	@SuppressWarnings("static-access")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		/**
		 * 方法一
		 */
		SharedPreferencesUtils1 sp = SharedPreferencesUtils1.getInstance();
		sp.init(this, "geek.xml");
		sp.setValue("name", "geek");
		
		String value = sp.getValue("name", "");
		Log.i(TAG, "value-->"+value);
		
		
		/**
		 * 方法二
		 */
		GDPreferences.getInstance().savePreference(GDPreferenceSettings.SETTING_IS_FIRST, false);
		boolean SETTING_IS_FIRST = GDPreferences.getSharedPreferences().getBoolean(GDPreferenceSettings.SETTING_IS_FIRST.getId(), (Boolean) GDPreferenceSettings.SETTING_IS_FIRST.getDefaultValue());
		
		Log.i(TAG, "SETTING_IS_FIRST-->"+SETTING_IS_FIRST);
	}

}



你可能感兴趣的:(Android基础)