| | |
| | | |
| | | import android.content.Context; |
| | | import android.content.SharedPreferences; |
| | | import android.util.Base64; |
| | | |
| | | |
| | | import com.hdl.sdk.common.HDLSdk; |
| | | |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.ObjectInputStream; |
| | | import java.io.ObjectOutputStream; |
| | | import java.util.Collections; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | |
| | | .apply(); |
| | | } |
| | | |
| | | private static String Object2String(Object object) { |
| | | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| | | ObjectOutputStream objectOutputStream = null; |
| | | try { |
| | | objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); |
| | | objectOutputStream.writeObject(object); |
| | | String string = new String(Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT)); |
| | | objectOutputStream.close(); |
| | | return string; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | private static Object String2Object(String objectString) { |
| | | byte[] mobileBytes = Base64.decode(objectString.getBytes(), Base64.DEFAULT); |
| | | ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes); |
| | | ObjectInputStream objectInputStream = null; |
| | | try { |
| | | objectInputStream = new ObjectInputStream(byteArrayInputStream); |
| | | Object object = objectInputStream.readObject(); |
| | | objectInputStream.close(); |
| | | return object; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public static void saveSerializableEntity(String key, Object saveObject) { |
| | | SharedPreferences.Editor editor = getAppPreference().edit(); |
| | | String string = Object2String(saveObject); |
| | | editor.putString(key, string); |
| | | editor.commit(); |
| | | } |
| | | |
| | | public static Object getSerializableEntity(String key) { |
| | | String string = getAppPreference().getString(key, null); |
| | | if (string != null) { |
| | | Object object = String2Object(string); |
| | | return object; |
| | | } else { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | |
| | | } |