-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreferenceHelper.java
More file actions
71 lines (65 loc) · 2.23 KB
/
PreferenceHelper.java
File metadata and controls
71 lines (65 loc) · 2.23 KB
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
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
public class PreferenceHelper {
private static final String LOG_TAG = PreferenceHelper.class.getSimpleName();
private static final String MODIFICATION_DATE = "_modificationdate";
private static final String PREF_NAME = "app_pref";
/**
*
* @param context
* @param key
* @param value
*/
public static void setPreference(Context context, String key, String value)
{
Log.d(LOG_TAG, "setPreference("+key+", "+value+")");
SharedPreferences sharedPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(key, value);
editor.putString(key + MODIFICATION_DATE, String.valueOf(System.currentTimeMillis()));
editor.commit();
}
/**
*
* @param context
* @param key
* @return
*/
public static boolean hasPreference(Context context, String key){
Log.d(LOG_TAG, "hasPreference("+key+")");
SharedPreferences sharedPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
return sharedPref.contains(key);
}
/**
*
* @param context
* @param key
*/
public static void removePreference(Context context, String key){
Log.d(LOG_TAG, "removePreference("+key+")");
SharedPreferences sharedPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
sharedPref.edit().remove(key).commit();
}
/**
*
* @param context
* @param key
* @return
*/
public static String getPreference(Context context, String key){
Log.d(LOG_TAG, "getPreference("+key+")");
SharedPreferences sharedPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
return sharedPref.getString(key, null);
}
/**
*
* @param context
* @param key
* @return
*/
public static String getPreferenceModificationDate(Context context, String key){
SharedPreferences sharedPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
return sharedPref.getString(key + MODIFICATION_DATE, null);
}
}