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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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);
    }

}