SharedPreferenceUtils.java
2.69 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.metroapp.forum;
import android.content.Context;
import android.content.SharedPreferences;
/**
* SharedPreference工具类
* Created by 赵天恩 on 2015/8/3.
*/
public class SharedPreferenceUtils {
public final static String SETTING = "Setting";
public static void putValue(Context context, String key, int value) {
if (context ==null){
return ;
}
SharedPreferences.Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putInt(key, value);
sp.commit();
}
public static void putValue(Context context, String key, boolean value) {
if (context ==null){
return ;
}
SharedPreferences.Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putBoolean(key, value);
sp.commit();
}
public static void putValue(Context context, String key, String value) {
if (context ==null){
return ;
}
SharedPreferences.Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putString(key, value);
sp.commit();
}
public static void putValue(Context context, String key, Long value) {
if (context ==null){
return ;
}
SharedPreferences.Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putLong(key, value);
sp.commit();
}
public static int getValue(Context context, String key, int defValue) {
if (context ==null){
return defValue;
}
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
int value = sp.getInt(key, defValue);
return value;
}
public static boolean getValue(Context context, String key, boolean defValue) {
if (context ==null){
return defValue;
}
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
boolean value = sp.getBoolean(key, defValue);
return value;
}
public static String getValue(Context context, String key, String defValue) {
if (context ==null){
return defValue;
}
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
String value = sp.getString(key, defValue);
return value;
}
public static Long getValue(Context context, String key, Long defValue) {
if (context ==null){
return defValue;
}
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
Long value = sp.getLong(key, defValue);
return value;
}
}