2016-09-24 3 views
0

ich bin auf der Suche nach Standort-Updates, sobald meine Anwendung beginnt mit (Netzwerk & GPS) -Provider, die Zeit dauert es für meine Standort-Empfänger, um die erste Position zu erhalten ist oft zu beheben lang für Benutzer warten. Bis ein genauerer Ort für meinen Standort-Listener bereitgestellt wird, , so denke ich, dass ich einen zwischengespeicherten Speicherort verwenden sollte, indem ich getLastKnownLocation (String) aufruft.Ich bekomme den Standort Provider und mobile Cache in android

aber wirklich bin ich ein bisschen verwirrend, wo genau ich getLastKnownLocation (String) -Methode in meinen Code setzen muss !!! vor oder nach requestLocationUpdates()? in meiner Funktion oder in onResume() ??

jemand mir bitte helfen, danke im voraus

das ist meine Funktion: -

private final LocationListener gpsLocationListener = new LocationListener() { 
    @Override 
    public void onLocationChanged(Location location) { 
      locationManager.removeUpdates(networkLocationListener); 
     Toast.makeText(activity,"New GPS location: " 
       + String.format("%9.6f", location.getLatitude()) + ", " 
       + String.format("%9.6f", location.getLongitude()) + "\n",Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     switch (status) { 
      case LocationProvider.AVAILABLE: 
       Toast.makeText(activity,"GPS available again\n",Toast.LENGTH_SHORT).show(); 
       break; 
      case LocationProvider.OUT_OF_SERVICE: 
       Toast.makeText(activity,"GPS out of service\n",Toast.LENGTH_SHORT).show(); 
       break; 
      case LocationProvider.TEMPORARILY_UNAVAILABLE: 
       Toast.makeText(activity,"GPS temporarily unavailable\n", Toast.LENGTH_SHORT).show(); 
       break; 
    }} 

    @Override 
    public void onProviderEnabled(String provider) { 
     Toast.makeText(activity,"GPS Provider Enabled\n",Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     Toast.makeText(activity,"GPS Provider Disabled\n",Toast.LENGTH_SHORT).show(); 
    } 
}; 


private final LocationListener networkLocationListener = new LocationListener() { 
    @Override 
    public void onLocationChanged(Location location) { 

     Toast.makeText(activity,"New network location: " 
       + String.format("%9.6f", location.getLatitude()) + ", " 
       + String.format("%9.6f", location.getLongitude()) + "\n",Toast.LENGTH_SHORT).show(); 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     switch (status) { 
      case LocationProvider.AVAILABLE: 
       Toast.makeText(activity,"Network location available again\n",Toast.LENGTH_SHORT).show(); 
       break; 
      case LocationProvider.OUT_OF_SERVICE: 
       Toast.makeText(activity,"Network location out of service\n",Toast.LENGTH_SHORT).show(); 
       break; 
      case LocationProvider.TEMPORARILY_UNAVAILABLE: 
       Toast.makeText(activity,"Network location temporarily unavailable\n",Toast.LENGTH_SHORT).show(); 
       break; 
     } 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     Toast.makeText(activity,"Network Provider Enabled\n",Toast.LENGTH_SHORT).show(); 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     Toast.makeText(activity,"Network Provider Disabled\n",Toast.LENGTH_SHORT).show(); 
    } 
}; 

und das ist Code in onResume() Funktion: -

locationManager = (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE); 

     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
       3000, 0, gpsLocationListener); 
    locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER, 5000, 0, 
      networkLocationListener); 
+0

Sie sollten es vor requestLocationUpdates() setzen, da es den letzten bekannten Standort festlegen und auf eine genauere Position warten wird. Zweitens sollten Sie es onResume() nicht aufrufen, da dies dazu führt, dass diese Methode immer wieder mit dem Lebenszyklus von Aktivität/Fragment aufgerufen wird. –

Antwort

0

lastKnownLocation() sein sollte Angerufen vor requestLocationUpdates() aber ich habe bemerkt, dass Sie removeUpdates() anrufen, sobald es einen Standort abgeholt hat. Vielleicht möchten Sie stattdessen requestSingleUpdate verwenden?

https://developer.android.com/reference/android/location/LocationManager.html#requestSingleUpdate(java.lang.String, android.app.PendingIntent)

Es soll beachtet werden, dass in this answer sagen, dass sie Sie für die GPS warten sollen, zu stabilisieren und requestSingleUpdate() Verwendung zu vermeiden, aber wenn Sie nicht nach großer Genauigkeit sind wäre es besser.

+0

Nein, ich möchte nicht 'requestSingleUpdate()' verwenden, ich rufe removeUpdates() auf, weil ich zuerst den "Netzwerk" -Anbieter verwende und dann auf "GPS" zurückwechsle, wenn es verfügbar ist, und den Netzwerkanbieter entfernen – 2NE1

Verwandte Themen