小编Rey*_*yaj的帖子

如何在SharedPreferences中存储和检索Location对象?

我想存储一个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)

java android sharedpreferences

2
推荐指数
1
解决办法
3824
查看次数

标签 统计

android ×1

java ×1

sharedpreferences ×1