2011-12-28 14 views
5
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); 
} 
} 

questo è il mio frammento di codice e il mio problema è che una volta che l'applicazione è installata con il GPS acceso, allora non è il passaggio a GSM a prendere posizione una volta che il GPS , e viceversa.Passaggio da gps e provider di rete in base alla disponibilità

Quindi, per favore dimmi un modo per passare in modo efficiente tra GPS e GSM in base alla disponibilità del fornitore di posizione.

NOTA: La mia app è un servizio.

Grazie.

Modifica: Ho ricevuto un BroadcastReceiver, ecco perché presumo che onCreate() venga richiamato dopo l'attivazione/disattivazione del GPS.

Ho anche provato a verificare la disponibilità del provider in onLocationChanged (posizione posizione). Ma non ha aiutato.

EDIT: ho modificato il mio onProviderDisabled & onProviderEnabled come questo, Si prega di dire ciò che sto facendo male qui ..

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

    } 
+0

sì qui ho trovato un altro problema, il mio onProviderEnabled() non viene chiamato. – Lenin

risposta

5

Hai una soluzione per questo:

public void onProviderDisabled(String provider) { 
    locationManager = (LocationManager) 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) 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); 
    } 
} 

quello che ho perso è locationManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE); in onProviderEnabled() e onProviderDisabled().

Grazie

Problemi correlati