我的问题很简单,我想保存并获取以前的位置(LatLang)在共享偏好中,当从GPS获得新位置时应该更换并获取功能应该在android中给出最新/新位置?请指导我
答案 0 :(得分:0)
尝试使用此代码
public class SharedPreferenceData {
private SharedPreferences settings;
private SharedPreferences.Editor editor;
private float latitude;
private longitude;
public SharedPreferenceData(Context context) {
settings=context.getApplicationContext().getSharedPreferences("MyDataFile",0);
editor=settings.edit();
}
public void PushData(float latitude, float longitude)
{
this.latitude=latitude;
this.longitude=longitude;
editor.putFloat("lat",latitude);
editor.putFloat("log",longitude);
editor.commit();
}
public float getlatitude()
{
return settings.getFloat("lat",0);
}
public float getlongitude()
{
return settings.getFloat("log",0);
}
}
然后使用位置监听器
SharedPreferenceData data=new SharedPreferenceData(this);
LocationListener locationListenerGPS=new LocationListener() {
@Override
public void onLocationChanged(android.location.Location location) {
double latitude=location.getLatitude();
double longitude=location.getLongitude();
data.PushData((float)latitude,(float)longitude);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};