AndroidのSharedPreferencesツールクラス
4771 ワード
本ツール類の永久メンテナンス、永久更新、もし読者の皆さんがバグや不合理な点を発見したら、伝言を残してください.
このツールクラスで提供される機能は、次のとおりです.
1.5種類のデータを保存する;
2.5種類のデータを読み取る.
内容は次のとおりです.
エンジニアリングサポートバージョン情報Android API Level>=11の場合、Setタイプのストレージと読み取りに参加することもできます.
Activityまたは他の場所での呼び出し方法は、次のようになります.
このツールクラスで提供される機能は、次のとおりです.
1.5種類のデータを保存する;
2.5種類のデータを読み取る.
内容は次のとおりです.
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
/**
*
* @author bear
*
* SharedPrefereces
*
*/
public class SharedPrefsUtil {
/**
* SharedPreferences int
*
* @param context
* @param name xml
* @param key
* @param value
*/
public static void putValue(Context context, String name, String key,
int value) {
Editor sp = getEditor(context, name);
sp.putInt(key, value);
sp.commit();
}
/**
* SharedPreferences boolean
*
* @param context
* @param name xml
* @param key
* @param value
*/
public static void putValue(Context context, String name, String key,
boolean value) {
Editor sp = getEditor(context, name);
sp.putBoolean(key, value);
sp.commit();
}
/**
* SharedPreferences String
*
* @param context
* @param name xml
* @param key
* @param value
*/
public static void putValue(Context context, String name, String key,
String value) {
Editor sp = getEditor(context, name);
sp.putString(key, value);
sp.commit();
}
/**
* SharedPreferences float
*
* @param context
* @param name xml
* @param key
* @param value
*/
public static void putValue(Context context, String name, String key,
float value) {
Editor sp = getEditor(context, name);
sp.putFloat(key, value);
sp.commit();
}
/**
* SharedPreferences long
*
* @param context
* @param name xml
* @param key
* @param value
*/
public static void putValue(Context context, String name, String key,
long value) {
Editor sp = getEditor(context, name);
sp.putLong(key, value);
sp.commit();
}
/**
* SharedPreferences int
*
* @param context
* @param name xml
* @param key
* @param defValue
* @return
*/
public static int getValue(Context context, String name, String key,
int defValue) {
SharedPreferences sp = getSharedPreferences(context, name);
int value = sp.getInt(key, defValue);
return value;
}
/**
* SharedPreferences boolean
*
* @param context
* @param name xml
* @param key
* @param defValue
* @return
*/
public static boolean getValue(Context context, String name, String key,
boolean defValue) {
SharedPreferences sp = getSharedPreferences(context, name);
boolean value = sp.getBoolean(key, defValue);
return value;
}
/**
* SharedPreferences String
*
* @param context
* @param name xml
* @param key
* @param defValue
* @return
*/
public static String getValue(Context context, String name, String key,
String defValue) {
SharedPreferences sp = getSharedPreferences(context, name);
String value = sp.getString(key, defValue);
return value;
}
/**
* SharedPreferences float
*
* @param context
* @param name xml
* @param key
* @param defValue
* @return
*/
public static float getValue(Context context, String name, String key,
float defValue) {
SharedPreferences sp = getSharedPreferences(context, name);
float value = sp.getFloat(key, defValue);
return value;
}
/**
* SharedPreferences long
*
* @param context
* @param name xml
* @param key
* @param defValue
* @return
*/
public static long getValue(Context context, String name, String key,
long defValue) {
SharedPreferences sp = getSharedPreferences(context, name);
long value = sp.getLong(key, defValue);
return value;
}
// Editor
private static Editor getEditor(Context context, String name) {
return getSharedPreferences(context, name).edit();
}
// SharedPreferences
private static SharedPreferences getSharedPreferences(Context context, String name) {
return context.getSharedPreferences(name, Context.MODE_PRIVATE);
}
}
エンジニアリングサポートバージョン情報Android API Level>=11の場合、Set
Activityまたは他の場所での呼び出し方法は、次のようになります.
// , MySetting ("color","red")
SharedPrefsUtil.putValue(this, "MySetting", "color", "red");
// , MySetting "color"
String color = SharedPrefsUtil.getValue(this, "MySetting", "color", "blue");