在android中存储少量数据的最佳方法

时间:2016-09-13 05:54:32

标签: android google-maps

我有一个非常基本的应用程序,它使用Maps API显示地图,长按后它会在该位置放置一个标记。如果您选择其他位置,它将删除当前标记并创建一个新标记。

该应用程序会执行我想要的操作,但我可以在应用程序关闭后继续保存数据。

这就是我目前正在尝试做的事情:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_maps);
      // Obtain the SupportMapFragment and get notified when the map is ready to be used.
      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
      mapFragment.getMapAsync(this);

      BufferedReader reader;

      try {
          final InputStream file = getAssets().open("userLatLng");
          reader = new BufferedReader(new InputStreamReader(file));
          String line = reader.readLine();
          line = reader.readLine();
          System.out.print("Getting Data");
      } catch (IOException ioe) {
          ioe.printStackTrace();
      }
  }

用于在应用程序启动后读取数据

            String filename = "userLatLng";
            FileOutputStream outputStream;

            try {
                outputStream = openFileOutput(filename, Context.MODE_PRIVATE);

                outputStream.write(Integer.parseInt(yayaParking.toString()));
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

应该是如何保存数据。我还没有让这个工作,所以任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:0)

您可以将其保存在sharedPreferences上。

    public String getLocation() {
        SharedPreferences pref; = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        return pref.getString("StoredLocation", "");
    }

    public void setLocation(String value) {
        SharedPreferences pref; = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        Editor editor = pref.edit();
        editor.putString("StoredLocation", value);
        editor.commit();
    }

您可以使用上面的代码存储和检索位置。

答案 1 :(得分:0)

创建SharedPreference的全局变量。

SharedPreferences preferences;

将您的数据另存为:

    private void savePreference(String your_data){

    preferences = getSharedPreferences("name_ur_preference", MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();

    editor.putString("DATA", your_data);

    editor.apply();
}

答案 2 :(得分:0)

使用SharedPreference API进行小型数据存储。

SharedPreferences sharedPref = context.getSharedPreferences(
        "lat_pref", Context.MODE_PRIVATE);

    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(KEY_LAT, "12.7333");   // your lat and lng
    editor.putString(KEY_LNG, "12.7333");
    editor.commit();

您可以像下面一样检索它

SharedPreferences sharedPref = context.getSharedPreferences("lat_pref",Context.MODE_PRIVATE);

String slat = sharedPref.getString(KEY_LAT, "0.0");  //0.0 is default value, you can change it to any value you like. This default value is applied when the key is not present in SharedPrefernce

String slng = sharedPref.getString(KEY_LNG, "0.0");

//Convert String Lat and Lng to double values
double lat = Double.parseDouble(slat);
double lng = Double.parseDouble(slng);

您可以声明KEY_LAT& KEY_LNG在一些常量文件中。

public static final String KEY_LAT = "latitude";
public static final String KEY_LNG = "longitude";

如果你在Activity中,那么使用this作为上下文,如果在Fragment中则使用getActivity()

编辑

editor.putLong(KEY_LAT, Double.doubleToLongBits(location.getLatitude()));

检索它,

double lat = Double.longBitsToDouble(sharedPref.getLong(KEY_LAT, 0); 

为经度做同样的事情

相关问题