-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreferenceUtils.java
57 lines (48 loc) · 2.01 KB
/
PreferenceUtils.java
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
package cn.lxw.us.utils;
import android.content.Context;
import android.content.SharedPreferences;
/**
* Created by Lianxw on 2015/7/31.
* Preference工具类
*/
public class PreferenceUtils {
private static final String PREFERENCE_NAME = "default_preference.xml";
public static SharedPreferences getPreference(Context context) {
return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
}
public static String getPreferenceString(Context context, String key) {
return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).getString(key,null);
}
public static Boolean getPreferenceBoolean(Context context,String key) {
return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).getBoolean(key, false);
}
public static SharedPreferences.Editor getPreferenceEditor(Context context) {
return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).edit();
}
public static String getPrivilegeString(Context context) {
return getPrivilegeString(context,false);
}
public static String getPrivilegeString(Context context,boolean withStore) {
SharedPreferences sp = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
if(!sp.getBoolean(Constants.PREF_LOGIN,false)) {
return null;
}
StringBuilder builder = new StringBuilder();
builder.append("from=mobile");
String accessKey = sp.getString(Constants.PREF_ACCESS_KEY,null);
if (accessKey!=null) {
builder.append("&access_key=").append(accessKey);
}
String uid = sp.getString(Constants.PREF_UID,null);
if (uid!=null) {
builder.append("&uid=").append(uid);
}
if (withStore) {
String storeId = sp.getString(Constants.PREF_STORE_ID,null);
if (storeId!=null) {
builder.append("&store_id=").append(storeId);
}
}
return builder.toString();
}
}