Blame view

src/com/ectrip/cyt/utils/SharedPreferences2Obj.java 6 KB
3c2353cd   杜方   1、畅游通核销app源码提交;
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
  package com.ectrip.cyt.utils;
  
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.StreamCorruptedException;
  
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.content.SharedPreferences.Editor;
  import android.os.Build;
  
  import com.ectrip.cyt.config.MyApp;
  
  /**
   * 2.0
   * SharedPreferences工具类
   */
  public class SharedPreferences2Obj {
  
      private static SharedPreferences2Obj preferences2Obj=null;
      private Context context;
      private SharedPreferences sp;
      int __sdkLevel=10;
  
      private SharedPreferences2Obj(Context context) {
          this.context = context;
      }
  
      /**
       * @param name
       * 必须设置名字
       */
      public SharedPreferences2Obj setName(String name){
          try {
              sp=MyApp.getInstance().savePreToSDcard(name);
          } catch (Exception e) {
              e.printStackTrace();
              try {
                  __sdkLevel= Build.VERSION.SDK_INT;
                  sp=context.getSharedPreferences(name,
                          (__sdkLevel > Build.VERSION_CODES.FROYO) ? 4 : 0);
              } catch (Exception e1) {
                  e1.printStackTrace();
              }
          }
          return this;
      }
  
      public static SharedPreferences2Obj getInstance(Context context){
          if(preferences2Obj==null){
              preferences2Obj=new SharedPreferences2Obj(context);
          }
          return preferences2Obj;
      }
  
  
      /**
       * 根据key和预期的value类型获取value的值
       *
       * @param key
       * @param clazz
       * @return
       */
      public <T> T getValue(String key, Class<T> clazz) {
          if (context == null) {
              throw new RuntimeException("请先调用带有context,name参数的构造!");
          }
          return getValue(key, clazz, sp);
      }
  
      /**
       * 清除所有数据
       */
      public void clear(){
          try {
              Editor editor = sp.edit();
              editor.clear();
              editor.commit();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public void clearKeyVal(String key){
          try {
              Editor editor = sp.edit();
              editor.putString(key,"");
              editor.commit();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      /**
       * 针对复杂类型存储<对象>
       *
       * @param key
       * @param val
       */
      public void setObject(String key, Object object) {
          ByteArrayOutputStream baos=null;
          ObjectOutputStream out = null;
          try {
              baos= new ByteArrayOutputStream();
              out = new ObjectOutputStream(baos);
              out.writeObject(object);
              String objectVal = new String(Base64.encode(baos.toByteArray()));
              Editor editor = sp.edit();
              editor.putString(key, objectVal);
              editor.commit();
  
          } catch (IOException e) {
              e.printStackTrace();
          }catch(Exception e){
              e.printStackTrace();
          }finally {
              try {
                  if (baos != null) {
                      baos.close();
                  }
                  if (out != null) {
                      out.close();
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  
      @SuppressWarnings("unchecked")
      public <T> T getObject(String key, Class<T> clazz) {
          if (sp.contains(key)) {
              String objectVal =null;
              byte[] buffer =null;
              ByteArrayInputStream bais=null;
              ObjectInputStream ois = null;
              try {
                  objectVal=sp.getString(key, null);
                  buffer= Base64.decode(objectVal);
                  bais = new ByteArrayInputStream(buffer);
                  ois = new ObjectInputStream(bais);
                  T t = (T) ois.readObject();
                  return t;
              } catch (StreamCorruptedException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (ClassNotFoundException e) {
                  e.printStackTrace();
              }catch(Exception e){
                  e.printStackTrace();
              } finally {
                  try {
                      if (bais != null) {
                          bais.close();
                      }
                      if (ois != null) {
                          ois.close();
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
          return null;
      }
  
      /**
       * 对于外部不可见的过渡方法
       *
       * @param key
       * @param clazz
       * @param sp
       * @return
       */
      @SuppressWarnings("unchecked")
      private <T> T getValue(String key, Class<T> clazz, SharedPreferences sp) {
          T t;
          try {
  
              t = clazz.newInstance();
  
              if (t instanceof Integer) {
                  return (T) Integer.valueOf(sp.getInt(key, 0));
              } else if (t instanceof String) {
                  return (T) sp.getString(key, "");
              } else if (t instanceof Boolean) {
                  return (T) Boolean.valueOf(sp.getBoolean(key, false));
              } else if (t instanceof Long) {
                  return (T) Long.valueOf(sp.getLong(key, 0L));
              } else if (t instanceof Float) {
                  return (T) Float.valueOf(sp.getFloat(key, 0L));
              }
          } catch (InstantiationException e) {
              e.printStackTrace();
              LogUtil.e("system", "类型输入错误或者复杂类型无法解析[" + e.getMessage() + "]");
          } catch (IllegalAccessException e) {
              e.printStackTrace();
              LogUtil.e("system", "类型输入错误或者复杂类型无法解析[" + e.getMessage() + "]");
          }catch(Exception e){
              e.printStackTrace();
              LogUtil.e("system", "无法找到" + key + "对应的值");
          }
  
          return null;
      }
  }