4

Locationオブジェクトを保存したいのですが、それを行うための良い方法を選択しようとしています。小さなオブジェクトが1つだけあり、それをプライベートにする必要があるため、これまでのところ、SharedPreferencesまたはInternalStorageが最も理にかなっています。

オブジェクトをbytearrayに書き込み、SharedPreferencesに文字列として格納してから、ここで説明するようにオブジェクトに書き戻すことができることがわかります。

それは合理的なアプローチですか?もっと良い方法はありますか?

アドバイスありがとうございます。

4

1 に答える 1

3

単純なデータを保存するときは、バイト配列を操作するよりもコードが単純で将来のメンテナにとって理解しやすいため、JSONオブジェクトを使用することを好みます。オブジェクトは小さいため、バイト配列の代わりに文字列を使用する場合のサイズのペナルティは重要ではありません。

private static final String LATITUDE = "com.somepackage.name.LATITUDE";
private static final String LONGITUDE = "com.somepackage.name.LONGITUDE";

/**
 * Save a location/key pair.
 * 
 * @param key the key associated with the location
 * @param location the location for the key
 * @return true if saved successfully false otherwise
 */
public boolean saveLocation(String key, Location location) {
    LOG.info("Saving location");
    try {
        JSONObject locationJson = new JSONObject();

        locationJson.put(LATITUDE, location.getLatitude());
        locationJson.put(LONGITUDE, location.getLongitude());
        //other location data
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString(key, locationJson.toString());
        edit.commit();
    } catch (JSONException e) {
        LOG.error("JSON Exception", e);
        return false;
    }

    LOG.info("Location {}  saved successfully at key: {}", preferences.getString(key, null),key);
    return true;
}

/**
 * Gets location data for a key.
 * 
 * @param key the key for the saved location
 * @return a {@link Location} object or null if there is no entry for the key
 */
public Location getLocation(String key) {
    LOG.info("Retrieving location at key {} ", key);
    try {
        String json = preferences.getString(key, null);

        if (json != null) {
            JSONObject locationJson = new JSONObject(json);
            Location location = new Location(STORAGE);
            location.setLatitude(locationJson.getInt(LATITUDE));
            location.setLongitude(locationJson.getInt(LONGITUDE));
            LOG.info("Returning location: {}" , location);
            return location;
        }
    } catch (JSONException e) {
        LOG.error("JSON Exception", e);
    }

    LOG.warn("No location found at key {}",  key);
    //or throw exception depending on your logic
    return null;
}
于 2012-09-10T10:36:00.070 に答える