SharedPreferences 저장 대상
                                            
 12827 단어  Android
                    
public class ShardPrefUtils {
    private static SharedPreferences mSharedPref;
    private static final String SHAREDPREF_NAME = Constant.SHAREDPREF_NAME;
/**
 * writeObject                ,      readObject        
 *   , Base64.encode        Base64     String 
 *
 * @param object        String   
 * @return String       String
 */
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;
    }
}
/**
 *   Base64  String,  Object  
 *
 * @param objectString     String
 * @return object          object
 */
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;
    }
}
/**
 *   SharedPreference    
 *
 * @param fileKey         key
 * @param key             key
 * @param saveObject      
 */
public static void saveSerializableEntity(String fileKey, String key, Object saveObject) {
    if(mSharedPref == null){
        mSharedPref = MyApplication.mContext.getSharedPreferences(SHAREDPREF_NAME, Context.MODE_PRIVATE);
    }
    SharedPreferences.Editor editor = mSharedPref.edit();
    String string = Object2String(saveObject);
    editor.putString(key, string);
    editor.commit();
}
/**
 *   SharedPreference     
 *
 * @param fileKey      key
 * @param key          key
 * @return object     key     
 */
public static Object getSerializableEntity(String fileKey, String key) {
    if(mSharedPref == null){
        mSharedPref = MyApplication.mContext.getSharedPreferences(SHAREDPREF_NAME, Context.MODE_PRIVATE);
    }
    String string = mSharedPref.getString(key, null);
    if (string != null) {
        Object object = String2Object(string);
        return object;
    } else {
        return null;
    }
}}위의 방법 은 Serializable 인 터 페 이 스 를 실현 해 야 합 니 다. Parcleble 인 터 페 이 스 를 실현 하 는 것 은 안 됩 니 다. 만약 에 Bitmap 와 같은 속성 이 있다 면 Serializable 을 사용 할 수 없습니다. Bitmap 능력 은 이미 Parcleable 인 터 페 이 스 를 실 현 했 기 때문에 있 으 면 따로 저장 해 야 합 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.