【发布时间】:2011-05-17 13:24:11
【问题描述】:
我有一个定期获取 GPS 更新的 Android 应用程序。如果我想将纬度、经度、速度、高度等存储在一个文件中,这样做的最佳做法是什么?
【问题讨论】:
标签: android file-io gps location
我有一个定期获取 GPS 更新的 Android 应用程序。如果我想将纬度、经度、速度、高度等存储在一个文件中,这样做的最佳做法是什么?
【问题讨论】:
标签: android file-io gps location
我建议您使用数据库——它们是为这样的任务而设计的。这是一个Android数据库教程:http://www.vogella.de/articles/AndroidSQLite/article.html
【讨论】:
每次收到地理修复时,将位置存储到文件中。在onLocationChanged 方法中调用类似
protected void storeLastKnownLocation(Location lastKnownLocation) {
//save last known location
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putFloat(LAST_KNOWN_LNG_KEY, (float) lastKnownLocation..getLongitude());
editor.putFloat(LAST_KNOWN_LAT_KEY, (float) lastKnownLocation.getLatitude());
editor.commit();
}
当 Activity 启动时,在 onCreate 方法中,您将检索这些值
float lastKnownLng = getPreferences(MODE_PRIVATE).getFloat(LAST_KNOWN_LNG_KEY, 0f);
float lastKnownLat = getPreferences(MODE_PRIVATE).getFloat(LAST_KNOWN_LAT_KEY, 0f);
...
【讨论】: