Dont verwenden, um die getLastKnownLocation weil das null oder alte Daten zurückkehrt sein könnte.
Dieser Code Ruft nur den Standort ab, sobald eine Taste gedrückt wird und nicht jedes Mal. Die Menschen in jedem Fall zu verlassen, verwenden hören die Lage Hörer und dass tötet die Lebensdauer der Batterie so das Code-Snippet verwenden ich tun viel Forschung Beiträge geschrieben haben:
// get the text view and buttons from the xml layout
Button button = (Button) findViewById(R.id.btnGetLocation);
final TextView latitude = (TextView) findViewById(R.id.textview4);
final TextView longitude = (TextView) findViewById(R.id.textview5);
final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
mlocation = location;
Log.d("Location Changes", location.toString());
latitude.setText(String.valueOf(location.getLatitude()));
longitude.setText(String.valueOf(location.getLongitude()));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Status Changed", String.valueOf(status));
}
@Override
public void onProviderEnabled(String provider) {
Log.d("Provider Enabled", provider);
}
@Override
public void onProviderDisabled(String provider) {
Log.d("Provider Disabled", provider);
}
};
// Now first make a criteria with your requirements
// this is done to save the battery life of the device
// there are various other other criteria you can search for..
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
// Now create a location manager
final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// This is the Best And IMPORTANT part
final Looper looper = null;
// Now whenever the button is clicked fetch the location one time
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationManager.requestSingleUpdate(criteria, locationListener, looper);
}
});
ihr das ist genau meine Frage, habe ich ein bis OnLocationChanged und ich möchte nur einen gültigen Speicherort bekommen .. wie kann ich den Strom von Updates stoppen? Gibt es einen Befehl? Danke – progdoc
Kurze Antwort LocationManager.removeUpdates(). Lange Antwort, siehe mein Update. – Sam
danke es funktionierte – progdoc