From 0ce9960bb073c10f8c12989291c94b98e6caf799 Mon Sep 17 00:00:00 2001 From: 562935844@qq.com Date: 星期四, 31 八月 2023 17:18:11 +0800 Subject: [PATCH] deviceinfo加判空 --- HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/common/utils/SPUtils.java | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 53 insertions(+), 0 deletions(-) diff --git a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/common/utils/SPUtils.java b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/common/utils/SPUtils.java index 17ceeab..60f6c5f 100644 --- a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/common/utils/SPUtils.java +++ b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/common/utils/SPUtils.java @@ -2,11 +2,17 @@ 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; @@ -146,5 +152,52 @@ .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; + } + } + } -- Gitblit v1.8.0