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

Rey*_*yaj 2 java android sharedpreferences

我想存储一个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 SharedPreferences
 * @return */
public Location getPrevLocation(Context context) {

    SharedPreferences settings;
    try {
        settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
        String jsonLocation = settings.getString(KEY_LOCATION, null);
        if (jsonLocation != null) {

            JSONObject locationJson = new JSONObject(jsonLocation);
            Location location = new Location("STORAGE");
            location.setLatitude(locationJson.getInt(LATITUDE));
            location.setLongitude(locationJson.getInt(LONGITUDE));
            Log.i("location_util_get", "Location" + location);
            return location;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

mav*_*oid 6

最佳解决方案:将location lat和long参数提取为字符串并将其保存在prefs中.您可以根据您的用例从位置提取其他信息.

保存位置: -

        if (location == null) {
            sharedPreferences.edit().removeKey("LOCATION_LAT").apply();
            sharedPreferences.edit().removeKey("LOCATION_LON").apply();
            sharedPreferences.edit().removeKey("LOCATION_PROVIDER").apply();
        } else {
            sharedPreferences.edit().putString("LOCATION_LAT", String.valueOf(location.getLatitude())).apply();
            sharedPreferences.edit().putString("LOCATION_LON", String.valueOf(location.getLongitude())).apply();
            sharedPreferences.edit().putString("LOCATION_PROVIDER", location.getProvider()).apply();
        }
Run Code Online (Sandbox Code Playgroud)

撤回地点: -

        String lat = sharedPreferences.getString("LOCATION_LAT", null);
        String lon = sharedPreferences.getString("LOCATION_LON", null);
        Location location = null;
        if (lat != null && lon != null) {
            String provider = sharedPreferences.getString("LOCATION_PROVIDER", null);
            location = new Location(provider);
            location.setLatitude(Double.parseDouble(lat));
            location.setLongitude(Double.parseDouble(lon));
        }
        return location;
Run Code Online (Sandbox Code Playgroud)

错误的解决方案:使用Gson来保留您的位置:

保存位置:

String json = location == null ? null : new Gson().toJson(location);
sharedPreferences.edit().putString("Location", json).apply();
Run Code Online (Sandbox Code Playgroud)

检索位置:

String json = sharedPreferences.getString("location", null);
return json == null ? null : new Gson().fromJson(json, Location.class);
Run Code Online (Sandbox Code Playgroud)

正如这个答案所述.

这不应该崩溃,但它会在某些设备上崩溃.如前所述这里.