Diese Klasse speichert Einstellungen nicht in den Shared Preferences, sondern in einer eigenen Datei im Dateisystem. Dies hat den Vorteil, dass die Einstellungen bei der Deinstallation der App immer gelöscht werden, was bei den Shared Preferences nicht immer der Fall ist.

Im Code muss vor der Verwendung noch die serialVersionUID gesetzt werden. Weiter Information hierzu gibt es in der Dokumentation von Oracle.

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.util.HashMap;

import android.content.Context;

public class SettingsManager {

    private static final long serialVersionUID = >>SERIAL_VERSION_UID<<;
    private static String saveFileName = SettingsManager.class.getSimpleName() + serialVersionUID + ".dat";

    private static final String tmpFileNameSuffix = ".tmp";

    private static boolean rename(Context context, String source, String destination) {
        File sourceFile = context.getFileStreamPath(source);
        File destinationFile = context.getFileStreamPath(destination);
        return sourceFile.renameTo(destinationFile);
    }

    private static boolean serialize(final Object obj, final String targetFileName, final Context context, boolean writeToTmpFileFirst) {
        String fileName = (writeToTmpFileFirst ? targetFileName + tmpFileNameSuffix : targetFileName);
        boolean success = true;

        FileOutputStream fos = null;
        ObjectOutputStream oos = null;

        try {
            fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
            if (!targetFileName.equals(fileName))
                success = rename(context, fileName, targetFileName);
        } catch (Exception e) {
            e.printStackTrace();
            success = false;
        } finally {
            if (oos != null) try {
                oos.close();
            } catch (Exception e) { }
            if (fos != null) try {
                fos.close();
            } catch (Exception e) { }
        }

        return success;
    }

    private static boolean serialize(final Object obj, final String fileName, final Context context) {
        return serialize(obj, fileName, context, true);
    }

    private static Object deserialize(String nameOfFile, Context context) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;

        try {
            fis = context.openFileInput(nameOfFile);
            ois = new ObjectInputStream(fis);
            Object obj = ois.readObject();
            ois.close();
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (ois != null) try {
                ois.close();
            } catch (Exception e) { }
            if (fis != null) try {
                fis.close();
            } catch (Exception e) { }
        }
    }

    private static void saveHashMap(Context context, HashMap<String, Object> hashMap) {
        serialize(hashMap, saveFileName, context);
    }

    @SuppressWarnings("unchecked")
    private static HashMap<String, Object> getHashMap(Context context) {
        Object o = deserialize(saveFileName, context);

        if (o instanceof HashMap<?, ?>) {
            return (HashMap<String, Object>) o;
        } else {
            return new HashMap<String, Object>();
        }
    }

    public static synchronized void putBoolean(Context context, String key, boolean value) {
        HashMap<String, Object> fileObj = getHashMap(context);
        fileObj.put(key, value);
        saveHashMap(context, fileObj);
    }

    public static synchronized void putInt(Context context, String key, int value) {
        HashMap<String, Object> fileObj = getHashMap(context);
        fileObj.put(key, value);
        saveHashMap(context, fileObj);
    }

    public static synchronized void putLong(Context context, String key, long value) {
        HashMap<String, Object> fileObj = getHashMap(context);
        fileObj.put(key, value);
        saveHashMap(context, fileObj);
    }

    public static synchronized void putFloat(Context context, String key, float value) {
        HashMap<String, Object> fileObj = getHashMap(context);
        fileObj.put(key, value);
        saveHashMap(context, fileObj);
    }

    public static synchronized void putString(Context context, String key, String value) {
        HashMap<String, Object> fileObj = getHashMap(context);
        fileObj.put(key, value);
        saveHashMap(context, fileObj);
    }

    public static synchronized void putDate(Context context, String key, Date value) {
        HashMap<String, Object> fileObj = getHashMap(context);
        fileObj.put(key, value);
        saveHashMap(context, fileObj);
    }

    public static synchronized void putHashMap(Context context, String key, HashMap<?, ?> value) {
        HashMap<String, Object> fileObj = getHashMap(context);
        fileObj.put(key, value);
        saveHashMap(context, fileObj);
    }

    public static synchronized void remove(Context context, String key) {
        HashMap<String, Object> fileObj = getHashMap(context);
        fileObj.remove(key);
        saveHashMap(context, fileObj);
    }

    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        HashMap<String, Object> fileObj = getHashMap(context);
        Object o = fileObj.get(key);
        if (o instanceof Boolean) {
            return (boolean) (Boolean) o;
        } else {
            return defaultValue;
        }
    }

    public static int getInt(Context context, String key, int defaultValue) {
        HashMap<String, Object> fileObj = getHashMap(context);
        Object o = fileObj.get(key);
        if (o instanceof Integer) {
            return (int) (Integer) o;
        } else {
            return defaultValue;
        }
    }

    public static long getLong(Context context, String key, long defaultValue) {
        HashMap<String, Object> fileObj = getHashMap(context);
        Object o = fileObj.get(key);
        if (o instanceof Long) {
            return (long) (Long) o;
        } else {
            return defaultValue;
        }
    }

    public static float getFloat(Context context, String key, float defaultValue) {
        HashMap<String, Object> fileObj = getHashMap(context);
        Object o = fileObj.get(key);
        if (o instanceof Float) {
            return (float) (Float) o;
        } else {
            return defaultValue;
        }
    }

    public static String getString(Context context, String key, String defaultValue) {
        HashMap<String, Object> fileObj = getHashMap(context);
        Object o = fileObj.get(key);
        if (o instanceof String) {
            return (String) o;
        } else {
            return defaultValue;
        }

    }

    public static Date getDate(Context context, String key, Date defaultValue) {
        HashMap<String, Object> fileObj = getHashMap(context);
        Object o = fileObj.get(key);
        if (o instanceof Date) {
            return (Date) o;
        } else {
            return defaultValue;
        }

    }

    public static HashMap<?, ?> getHashMap(Context context, String key, HashMap<?, ?> defaultValue) {
        HashMap<String, Object> fileObj = getHashMap(context);
        Object o = fileObj.get(key);
        if (o instanceof HashMap<?, ?>) {
            return (HashMap<?, ?>) o;
        } else {
            return defaultValue;
        }

    }

    public static boolean contains(Context context, String key) {
        HashMap<String, Object> fileObj = getHashMap(context);
        return fileObj.containsKey(key);
    }

}