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