Android Open Source - ihatecolor_and Shared Preference Util






From Project

Back to project page ihatecolor_and.

License

The source code is released under:

Copyright (c) 2013, Sewon Ann All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * ...

If you think the Android project ihatecolor_and listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package kr.pe.kingori.ihatecolor.util;
//from   ww  w.  j  a  v a 2 s.  c o m
import android.content.Context;
import android.content.SharedPreferences;
import kr.pe.kingori.ihatecolor.Application;

import java.util.HashMap;

public class SharedPreferenceUtil {
    private static HashMap<String, Object> memoryCache = new HashMap<String, Object>();
    private static final String SHARED_PREFERENCE_NAME = "game_app";

    private SharedPreferenceUtil() {
    }

    public static void put(String key, int value) {
        memoryCache.put(key, value);
        getSharedPreference().edit().putInt(key, value).commit();
    }

    public static void put(String key, String value) {
        memoryCache.put(key, value);
        getSharedPreference().edit().putString(key, value).commit();
    }

    public static void put(String key, long value) {
        memoryCache.put(key, value);
        getSharedPreference().edit().putLong(key, value).commit();
    }

    public static int getInt(String key) {
        Integer result = getValueFromCache(key);
        if (result == null) {
            result = getSharedPreference().getInt(key, 0);
            memoryCache.put(key, result);
        }
        return result;
    }

    public static long getLong(String key) {
        Long result = getValueFromCache(key);
        if (result == null) {
            result = getSharedPreference().getLong(key, 0L);
            memoryCache.put(key, result);
        }
        return result;
    }

    public static String getString(String key) {
        String result = getValueFromCache(key);
        if (result == null) {
            result = getSharedPreference().getString(key, null);
            memoryCache.put(key, result);
        }
        return result;
    }

    public static <T> T getValueFromCache(String key) {
        if (memoryCache.containsKey(key)) {
            return (T) memoryCache.get(key);
        } else {
            return null;
        }
    }

    private static SharedPreferences getSharedPreference() {
        return Application.context
                .getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
    }

}




Java Source Code List

com.google.example.games.basegameutils.BaseGameActivity.java
com.google.example.games.basegameutils.GameHelper.java
kr.pe.kingori.ihatecolor.Application.java
kr.pe.kingori.ihatecolor.model.Color.java
kr.pe.kingori.ihatecolor.model.GameMode.java
kr.pe.kingori.ihatecolor.ui.Constants.java
kr.pe.kingori.ihatecolor.ui.CustomDialogFragment.java
kr.pe.kingori.ihatecolor.ui.activity.MainActivity.java
kr.pe.kingori.ihatecolor.ui.activity.SplashActivity.java
kr.pe.kingori.ihatecolor.ui.event.DialogEvent.java
kr.pe.kingori.ihatecolor.ui.event.GameEvent.java
kr.pe.kingori.ihatecolor.ui.event.PlayEvent.java
kr.pe.kingori.ihatecolor.ui.fragment.BaseFragment.java
kr.pe.kingori.ihatecolor.ui.fragment.GameFragment.java
kr.pe.kingori.ihatecolor.ui.fragment.MainFragment.java
kr.pe.kingori.ihatecolor.ui.fragment.WaitingFragment.java
kr.pe.kingori.ihatecolor.ui.view.CustomFontButton.java
kr.pe.kingori.ihatecolor.ui.view.CustomFontTextView.java
kr.pe.kingori.ihatecolor.ui.view.QuestionViewGroup.java
kr.pe.kingori.ihatecolor.util.FontManager.java
kr.pe.kingori.ihatecolor.util.SharedPreferenceUtil.java
kr.pe.kingori.ihatecolor.util.UiUtil.java