Instantly share code, notes, and snippets.
Created
March 6, 2018 09:06
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save milaptank/97cdbccbd4de7f763112c0cc35d7a0de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.core.storage; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.preference.PreferenceManager; | |
import java.util.Map; | |
import java.util.Set; | |
/** | |
* @author Milap Tank | |
* Email:[email protected] | |
* @desc SharedPreferenceUtil.java is for | |
* @since 13/09/17 11:47 PM | |
*/ | |
public class SharedPreferenceUtil { | |
//https://www.dropbox.com/s/zai0vy1jcm271m8/main.zip?dl=0 | |
private static SharedPreferences sharedPreferences = null; | |
private static SharedPreferences.Editor editor = null; | |
/** | |
* Initialize the SharedPreferences instance for the app. | |
* This method must be called before using any | |
* other methods of this class. | |
* | |
* @param context {@link Context} | |
*/ | |
public static void init(Context context) { | |
if (sharedPreferences == null) { | |
sharedPreferences = PreferenceManager | |
.getDefaultSharedPreferences(context); | |
editor = sharedPreferences.edit(); | |
} | |
} | |
/** | |
* Initialize the SharedPreferences instance for the app. | |
* This method must be called before using any | |
* other methods of this class. | |
* | |
* @param context {@link Context} | |
* @param name name of preference {@link String } | |
* @param mode mode of preference | |
*/ | |
/* public static void init(Context context, String name, int mode) { | |
if (sharedPreferences == null) { | |
sharedPreferences = context.getSharedPreferences(name, mode); | |
editor = sharedPreferences.edit(); | |
} | |
}*/ | |
/** | |
* Initialize the SharedPreferences instance for the app. | |
* This method must be called before using any | |
* other methods of this class. | |
* | |
* @param context {@link Context} | |
* @param name name of preference {@link String } | |
*/ | |
/* public static void init(Context context, String name) { | |
init(context, name, MODE_PRIVATE); | |
}*/ | |
/** | |
* Puts new Key and its Values into SharedPreference map. | |
* | |
* @param key SharedPreference key | |
* @param value Set StringSet | |
*/ | |
public static void putValue(String key, Set<String> value) { | |
editor.putStringSet(key, value); | |
} | |
/** | |
* Puts new Key and its Values into SharedPreference map. | |
* | |
* @param key SharedPreference key | |
* @param value Save String value | |
*/ | |
public static void putValue(String key, String value) { | |
editor.putString(key, value); | |
} | |
/** | |
* Puts new Key and its Values into SharedPreference map. | |
* | |
* @param key SharedPreference key | |
* @param value save int value | |
*/ | |
public static void putValue(String key, int value) { | |
editor.putInt(key, value); | |
} | |
/** | |
* Puts new Key and its Values into SharedPreference map. | |
* | |
* @param key SharedPreference key | |
* @param value save long value | |
*/ | |
public static void putValue(String key, long value) { | |
editor.putLong(key, value); | |
} | |
/** | |
* Puts new Key and its Values into SharedPreference map. | |
* | |
* @param key SharedPreference key | |
* @param value save boolean value | |
*/ | |
public static void putValue(String key, boolean value) { | |
editor.putBoolean(key, value); | |
} | |
/** | |
* saves the values from the editor to the SharedPreference and it is must | |
*/ | |
public static void save() { | |
editor.apply(); | |
} | |
/** | |
* returns a values associated with a Key default value "" | |
* | |
* @return String | |
*/ | |
public static String getString(String key, String defValue) { | |
return sharedPreferences.getString(key, defValue); | |
} | |
/** | |
* returns a values associated with a Key default value "" | |
* | |
* @return String Set | |
*/ | |
public static Set<String> getStringSet(String key, Set<String> defValue) { | |
return sharedPreferences.getStringSet(key, defValue); | |
} | |
/** | |
* returns a values associated with a Key default value -1 | |
* | |
* @return String | |
*/ | |
public static int getInt(String key, int defValue) { | |
return sharedPreferences.getInt(key, defValue); | |
} | |
/** | |
* returns a values associated with a Key default value -1 | |
* | |
* @return String | |
*/ | |
public static long getLong(String key, long defValue) { | |
return sharedPreferences.getLong(key, defValue); | |
} | |
/** | |
* returns a values associated with a Key default value false | |
* | |
* @return String | |
*/ | |
public static boolean getBoolean(String key, boolean defValue) { | |
return sharedPreferences.getBoolean(key, defValue); | |
} | |
/** | |
* Checks if key is exist in SharedPreference | |
* | |
* @param key SharedPreference key | |
* @return boolean | |
*/ | |
public static boolean contains(String key) { | |
return sharedPreferences.contains(key); | |
} | |
/** | |
* @param key SharedPreference key | |
* @return | |
*/ | |
public static void remove(String key) { | |
editor.remove(key); | |
editor.apply(); | |
} | |
/** | |
* returns map of all the key value pair available in SharedPreference | |
* | |
* @return Map<String, ?> | |
*/ | |
public static Map<String, ?> getAll() { | |
return sharedPreferences.getAll(); | |
} | |
public static void clear() { | |
editor.clear(); | |
editor.apply(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment