can you change between GPS_PROVIDER and NETWORK_PROVIDER at runtime?

1

The situation is this, every so often I send the user's location to a small test table, the location I get it through the gps, if this was not activated at the start of the service I would get the location through NETWORK_PROVIDER, all it works perfect the detail here is that if I'm using the gps and I deactivate it does not send the location of the NETWORK_PROVIDER, I think it's because the service's onCreate is created like this, but at the time of execution I can not change what changes between a provider or another

servicioUbicacion.java

public class servicioUbicacion extends Service {
private static final String TAG = "ProyectoFinal";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 3000;
private static final float LOCATION_DISTANCE = 10f;

private class LocationListener implements android.location.LocationListener{
        Location mLastLocation;
        String ubicacion="";
        private Context mContext;
        Preferencias preff;
        int id_usuario;

        public LocationListener (String provider){
             Log.e(TAG, "LocationListener " + provider);
             mLastLocation = new Location(provider);
        }

        @Override
        public void onLocationChanged(Location location) {
            //aqui se registra
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.e(TAG,"onProviderChanged"+ provider);
        }
        @Override
        public void onProviderEnabled(String provider) {
            Log.e(TAG,"onProviderEnabled"+ provider);
        }
        @Override
        public void onProviderDisabled(String provider) {
            if(provider.equals("gps")){
                Log.e(TAG,"onProviderDisabled"+ provider);
            }
        }
}
LocationListener[] mLocationListeners = new LocationListener[]{
        new LocationListener(LocationManager.GPS_PROVIDER,this),
        new LocationListener(LocationManager.NETWORK_PROVIDER,this)
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
@Override
public int onStartCommand(Intent intent,int flags, int startId) {
    Log.e(TAG,"onStartComand");
    super.onStartCommand(intent,flags,startId);
    return START_STICKY;
}
@Override
public void onCreate() {
    Log.e(TAG, "onCreate");    
    preff = new Preferencias(getApplicationContext());
    id_usuario; = preff.misPreferencias.getInt(idUsuario,0);
   initializeLocationManager();
    try{
        if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            mLocationManager.requestLocationUpdates(    LocationManager.GPS_PROVIDER,LOCATION_INTERVAL,LOCATION_DISTANCE,mLocationListeners[0]);
        }else{
            mLocationManager.requestLocationUpdates(    LocationManager.NETWORK_PROVIDER,LOCATION_INTERVAL,LOCATION_DISTANCE,mLocationListeners[1]);
        }        
    }catch (java.lang.SecurityException ex){
        Log.i(TAG,"Fallo la obtencion de la ubicacion, ",ex);
    }catch (IllegalArgumentException ex){
        Log.d(TAG,"Provedor de red no existe, "+ex.getMessage());
    }
}

@Override
public void onDestroy() {
    Log.e(TAG, "onDestroy");
    super.onDestroy();
    if (mLocationManager != null) {
        for (int i = 0; i < mLocationListeners.length; i++) {
            try {
                mLocationManager.removeUpdates(mLocationListeners[i]);
            } catch (Exception ex) {
                Log.i(TAG, "Fallo en remover los datos de ubicacion , ignore", ex);
            }
        }
    }
}

private void initializeLocationManager(){
    Log.e(TAG,"inicia la localizacion");
    if(mLocationManager==null){
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(  Context.LOCATION_SERVICE);
    }
}}
    
asked by JesusSh 25.10.2017 в 23:30
source

1 answer

0

Your question is:

  

Can you switch between GPS_PROVIDER and NETWORK_PROVIDER in time of   execution?

The answer is, yes, you can do it.

But to get the geolocation regularly, it is checked if one of the two providers is active, which is making your code and it is correct:

if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,LOCATION_INTERVAL,LOCATION_DISTANCE,mLocationListeners[0]);
}else{
    mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,LOCATION_INTERVAL,LOCATION_DISTANCE,mLocationListeners[1]);
}

If you do not get the location using the GPS provider then try for the one from the Network, if it is not active either, there is no way to obtain it.

You must ensure you have at least one of the providers to get the geolocation.

To detect if the providers are enabled you can do it this way:

  

NETWORK_PROVIDER:

boolean network_provider_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  

GPS_PROVIDER:

boolean gps_provider_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    
answered by 25.10.2017 в 23:39