Ola a todos, estou montando um aplicativo para que usa os dados do GPS nativo e guarda a latitude e longitude no sharedpreferences, porem so estou conseguindo pegar uma vez estes dados, segue abaixo como esta meu codigo no onchange:
public void onLocationChanged(Location location) {
SharedPreferences preferencias = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = preferencias.edit();
editor.putString("latitude",Double.toString(location.getLatitude()));
editor.putString("longitude",Double.toString(location.getLongitude()));
editor.commit();
Log.w("Latitude Changed", Double.toString(location.getLatitude()));
Log.w("Longitude Changed", Double.toString(location.getLongitude()));
Log.w("Coordenadas Alteradas", "Salvo nas preferencias");
}
e onde estou buscando os dados:
[code] public void onClick(View arg0) {
// TODO Auto-generated method stub
//simpleFormat = new SimpleDateFormat(“HH:mm:ss”);
SharedPreferences preferencias = getSharedPreferences(PREFS_NAME, 0);
if((tg.isChecked()))
{
try {
//SharedPreferences preferencias = getSharedPreferences(PREFS_NAME, 0);
OLatitude = Double.valueOf(preferencias.getString("latitude", "").replaceAll(",", "."));
OLongitude = Double.valueOf(preferencias.getString("longitude", "").replaceAll(",", "."));
Log.w("Latitude Origem", Double.toString(OLatitude));
Log.w("Longitude Origem", Double.toString(OLongitude));
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Sua localização é - \nLatitude: " + OLatitude + "\nLongitude: " + OLongitude, Toast.LENGTH_LONG).show();
Date dataAtual = new Date();
Hinicio = (new SimpleDateFormat("HH:mm").format(dataAtual));
HinicioFormatada = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dataAtual));
Log.w("Hora Atual formatada", HinicioFormatada);
ativaTimer();
}
else
{
try {
//SharedPreferences preferencias = getSharedPreferences(PREFS_NAME, 0);
OLatitude = Double.valueOf(preferencias.getString("latitude", "").replaceAll(",", "."));
OLongitude = Double.valueOf(preferencias.getString("longitude", "").replaceAll(",", "."));
Log.w("Latitude Destino", Double.toString(DLatitude));
Log.w("Longitude Destino", Double.toString(DLongitude));
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Sua localização é - \nLatitude: " + DLatitude + "\nLongitude: " + DLongitude, Toast.LENGTH_LONG).show();
task.cancel();
Date dataAtual = new Date();
Hfinal = (new SimpleDateFormat("HH:mm").format(dataAtual));
HfinalFormatada = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dataAtual));
Log.w("Hora final formatada", HfinalFormatada);
}
}[/code]
Quando clico no toglebutton, a primeira vez(check) ele busca os dados certo, e quando desmarco(uncheck) o toglebutton ele retorna 0 olha o logcat:
02-06 14:18:45.319: W/Latitude Origem(18947): -24.70854957
02-06 14:18:45.319: W/Longitude Origem(18947): -53.76809223
02-06 14:18:45.419: W/Hora Atual formatada(18947): 2013-02-06 14:18:45
02-06 14:18:47.749: W/Latitude Changed(18947): -24.7086452
02-06 14:18:47.759: W/Longitude Changed(18947): -53.76786307
02-06 14:18:47.759: W/Coordenadas Alteradas(18947): Salvo nas preferencias
02-06 14:18:48.469: W/Tempo Atual(18947): 0
02-06 14:18:48.479: W/Hora total formatada(18947): 2013-02-06 14:18:48
02-06 14:18:51.459: W/Tempo Atual(18947): 0
02-06 14:18:51.469: W/Hora total formatada(18947): 2013-02-06 14:18:51
02-06 14:18:52.139: W/Latitude Destino(18947): 0.0
02-06 14:18:52.139: W/Longitude Destino(18947): 0.0