我想编组和解组一个实现Parcelable/来自字节数组的类.我很清楚Parcelable表示不稳定,因此不适用于实例的长期存储.但我有一个用例,我需要序列化一个对象,如果由于内部Android更改而解组失败,它不是一个showstopper.该类已经在实现该Parcelable接口.
给定一个类MyClass implements Parcelable,我如何(ab)使用Parcelable接口进行编组/解组?
我正在使用android/java中的Location子类序列化打砖块
位置不可序列化.我有一个名为FALocation的第一个子类,它没有任何实例变量.我已声明它可序列化.
然后我有一个名为Waypoint的第二个类看起来像这样:
public class Waypoint extends FALocation implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/* Class variables *******************************************************/
private static int CLASS_VERSION=1; //Used to version parcels
/* Instance variables ****************************************************/
private transient String type=DataHelper.PT_TYPE_US;
private transient String country;
private transient String name=null;
private transient String description=null;
private transient int elevation = 0;
private transient int population = 0; // Afterthought, added to match the DB structure
/* Constructors **********************************************************/
public Waypoint() {
super(); …Run Code Online (Sandbox Code Playgroud) 我想存储一个Location对象,我正在尝试选择一个很好的方法.请告诉我如何做到这一点
我正在使用此代码,但是当我从首选项获取位置时,位置会像这样返回...
位置[mProvider = STORAGE,MTIME = 0,mLatitude = 30.0,mLongitude = 76.0,mHasAltitude =假,mAltitude = 0.0,mHasSpeed =假,MSPEED = 0.0,mHasBearing =假,mBearing = 0.0,mHasAccuracy =假,mAccuracy = 0.0, mExtras =空]
/** Store Location object in SharedPreferences */
public void storeLocation(Context context, Location location) {
SharedPreferences settings;
Editor editor;
try {
JSONObject locationJson = new JSONObject();
locationJson.put(LATITUDE, location.getLatitude());
locationJson.put(LONGITUDE, location.getLongitude());
settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
editor = settings.edit();
editor.putString(KEY_LOCATION, locationJson.toString());
editor.commit();
Log.i("location_util_store", "Location" + location);
} catch (Exception e) {
e.printStackTrace();
}
}
/** Retrieve Location object from …Run Code Online (Sandbox Code Playgroud)