hallo zusammen,
ich habe meine Activity umgebaut und nutze nun beide LocationListener.
Die lassen sich auch sauber entfernen und solange animateTo nichtausgeführt wird, wird auch das GPS-Dings abgeschaltet.
Das Problem, dass das Ding weiter läuft, sobald animateTo ausgeführt wurde ist aber leider nicht behoben.
Ich habe die komplette Activity mal beigefügt.
Hat noch wer ne Idee?
Ich danke euch schon mal.
EDIT: wie kriegt man denn den Code in den Tags richtig formatiert ?
Java
import java.util.List;
import android.content.Context;import android.content.SharedPreferences;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.location.LocationProvider;import android.os.Bundle;import android.preference.PreferenceManager;import android.view.View;import android.widget.LinearLayout;import com.google.android.maps.GeoPoint;import com.google.android.maps.MapActivity;import com.google.android.maps.MapController;import com.google.android.maps.MapView;import com.google.android.maps.MapView.LayoutParams; import com.google.android.maps.MyLocationOverlay;import com.google.android.maps.Overlay;
public class pKarte extends MapActivity {
private MapView mapView; private MapController mapController; private GeoPoint point; public boolean routenverfolgung = false; private LocationManager locationManager; private int gpsStatus = 0; // 1 = AVAILABLE 2 = OUT_OF_SERVICE 3 = TEMPORARILY_UNAVAILABLE private int networkStatus = 0; // 1 = AVAILABLE 2 = OUT_OF_SERVICE 3 = TEMPORARILY_UNAVAILABLE private boolean gpsProviderEnabled = false; private boolean networkProviderEnabled = false; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // main.xml contains a MapView setContentView(R.layout.map_layout); LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom); // extract MapView from layout mapView = (MapView) findViewById(R.id.mapView); mapView.setBuiltInZoomControls(true); mapController = mapView.getController(); View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mapView.displayZoomControls(true);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); } @Override protected void onResume() { super.onResume(); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, networkLocationListener); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, gpsLocationListener); }
@Override protected void onPause() { super.onPause(); locationManager.removeUpdates(networkLocationListener); locationManager.removeUpdates(gpsLocationListener); locationManager = null; }
@Override protected boolean isRouteDisplayed() { // TODO Automatisch generierter Methodenstub return false; } private final LocationListener gpsLocationListener = new LocationListener() {
@Override public void onStatusChanged(String provider, int status, Bundle extras) { switch (status) { case LocationProvider.AVAILABLE: gpsStatus = 1; break; case LocationProvider.OUT_OF_SERVICE: gpsStatus = 2; break; case LocationProvider.TEMPORARILY_UNAVAILABLE: gpsStatus = 3; break; } }
@Override public void onProviderEnabled(String provider) { gpsProviderEnabled = true; }
@Override public void onProviderDisabled(String provider) { gpsProviderEnabled = false; }
@Override public void onLocationChanged(Location location) { locationManager.removeUpdates(networkLocationListener); if (location != null) { // Do something knowing the location changed by the distance you requested doesSomethingWithNewLocation(location); } } };
private final LocationListener networkLocationListener = new LocationListener() {
@Override public void onStatusChanged(String provider, int status, Bundle extras) { switch (status) { case LocationProvider.AVAILABLE: networkStatus = 1; break; case LocationProvider.OUT_OF_SERVICE: networkStatus = 2; break; case LocationProvider.TEMPORARILY_UNAVAILABLE: networkStatus = 3; break; } }
@Override public void onProviderEnabled(String provider) { networkProviderEnabled = true;
}
@Override public void onProviderDisabled(String provider) { networkProviderEnabled = false; }
@Override public void onLocationChanged(Location location) { if (location != null) { // Do something knowing the location changed by the distance you requested doesSomethingWithNewLocation(location); }
} }; public void doesSomethingWithNewLocation(Location loc) { Location location = loc; // Update the map location. Double geoLat = location.getLatitude()*1E6; Double geoLng = location.getLongitude()*1E6; point = new GeoPoint(geoLat.intValue(), geoLng.intValue()); mapController.animateTo(point); // mapController.setZoom(17); // Add the MyLocationOverlay List<Overlay> listOfOverlays = mapView.getOverlays(); MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView); listOfOverlays.add(myLocationOverlay); myLocationOverlay.enableMyLocation(); mapView.invalidate(); }
}
Display More