Der Wechsel zwischen gps-und Netzwerk-Anbieter nach der Verfügbarkeit
public void onCreate() {
locationListener = new GeoUpdateHandler();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if(gps_enabled) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 100, locationListener);
} else if(network_enabled) { //Checking for GSM
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, DISTANCE, locationListener);
}
}
public class GeoUpdateHandler implements LocationListener {
private void getPresentLocation(Location location) {
...
//Fetch the locations
...
}
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
@Override
public void onLocationChanged(Location location) {
getPresentLocation(location);
}
}
dies ist mein code-snippet und mein Problem ist, dass sobald die Anwendung installiert ist, mit GPS Eingeschaltet ist, dann ist es nicht der Wechsel von GSM zu Holen, Position, sobald wir AUS GPS-und Umgekehrt.
Also bitte Sag es mir, zu wechseln, effizient zwischen GPS-und GSM-entsprechend der Verfügbarkeit der location-Anbieter.
HINWEIS: Meine app ist eine Dienstleistung.
Dank.
Edit: ich habe einen BroadcastReceiver, das ist, warum ich gehe davon aus das onCreate() wird aufgerufen, sobald die GPS-ON/OFF passieren.
Ich habe auch versucht die Kontrolle der Anbieter die Verfügbarkeit in onLocationChanged(Location location).
Aber es hat nicht geholfen.
EDIT: ich meine modifizierten onProviderDisabled & onProviderEnabled wie diese,
Bitte sagen Sie, was mache ich hier falsch..
public void onProviderDisabled(String provider) {
locationManager.removeUpdates(locationListener);
if (provider.toLowerCase().equals(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, 1, locationListener);
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 1, locationListener);
}
}
@Override
public void onProviderEnabled(String provider) {
locationManager.removeUpdates(locationListener);
if (provider.toLowerCase().equals(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 1, locationListener);
} else {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, 1, locationListener);
}
}
- ja, hier fand ich ein weiteres problem, mein onProviderEnabled() nicht immer aufgerufen.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Bekam dafür eine Lösung:
was ich verpasst habe ist
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
in onProviderEnabled() und onProviderDisabled().
Dank
Sieht aus wie Sie sind nur Interesse an den aktuellen Standort in der onCreate () - Methode. Sobald Anbieter zu wechseln, müssen Sie auf Anfrage updates von dem neuen Anbieter, keine?
Hier ein Beitrag auf Android-blog über Standorte, es deckt monitoring-Anbieter änderungen zu:
http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html