[英]Best way to store small amounts of data in android
我有一個非常基本的應用程序,它使用 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();
}
這應該是它保存數據的方式。 我還沒有讓它工作,所以任何幫助將不勝感激!
您可以將其保存在 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();
}
您可以使用上面的代碼存儲和檢索位置。
創建一個 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();
}
使用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);
對經度做同樣的事情
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.