Im Folgenden erfahren Sie, wie ich den LocationListener hinzufüge. Es wurde zuvor als LocationListener locationListener deklariert.
Code: Select all
private void addLocationListener(){
locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
GeoPoint userLoc = processNewLocation(location);
if(userLoc != null){
Log.d("USERLOC", userLoc.toString());
//do something with the location
}
}
@Override
public void onProviderDisabled(String provider) {
locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}
@Override
public void onProviderEnabled(String provider) {
locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
}
Hier ist der Code, mit dem der Hörer beim Standortmanager registriert wird
Code: Select all
public void addLocationManager(){
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
}
private void getLocationFromNetwork(){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, locationListener);
Log.d("GET NETWORK", "Listener registered");
}
private void getLocationFromGPS(){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, locationListener);
Log.d("GET GPS", "Listener registered");
}
private Location getLastLocation(){
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
return lastKnownLocation;
}
Mobile version