Verwendung von LocationListener innerhalb eines FragmentsAndroid

Forum für diejenigen, die für Android programmieren
Anonymous
 Verwendung von LocationListener innerhalb eines Fragments

Post by Anonymous »

Kann ich den Standort eines Benutzers mithilfe von LocationListener und LocationManager aus einem Fragment heraus ermitteln? Oder muss ich den LocationListener innerhalb der zugrunde liegenden Aktivität verwenden und eine Schnittstelle (oder eine andere Methode) verwenden, um die Daten zurück zum Fragment zu übertragen? Ich bin gerade dabei, eine Aktivität aufgrund einer Änderung der App-Benutzeroberfläche in ein Fragment umzuwandeln. Das Abrufen des Standorts war kein Problem, als ich eigenständige Aktivitäten verwendet habe. Allerdings kann ich jetzt, da ich die Aktivität in ein Fragment umgewandelt habe, keinen Standort mehr zurückerhalten.

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

}

};
}
HINWEIS: ProcessNewLocation konvertiert lediglich den Standort in einen GeoPoint. Es kommt noch nicht einmal so weit, da nie ein Standort ermittelt wird.

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;
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post