Is it possible to deactivate the gps to reduce the battery consumption in an app?

2

I am using the GPS in my application to define the exact location of Smartphone , ( latitud and longitud ), coupled with this I receive the coordinates in a Geocoder to get the name of the street according to the coordinates, the problem is that the application consumes a lot of battery, so I would like the coordinate update to be executed that I am using only 3 times each time I execute the OnClickListener , and when I finish doing it the GPS deactivate, or that the application stops executing the method that calls the coordinate update by means of GPS , how could it do it? Any suggestions?

Here my code:

GPS.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FuntionGPS();
            Dialog();
        }
    });

public void FuntionGPS() {
        LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Localizacion Local = new Localizacion();
        Local.setGPSActivity(this);
        assert mlocManager != null;
        boolean gpsEnabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (!gpsEnabled) {
            Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(settingsIntent);
        }
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 1000);
            return;
        }
        mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 20 * 1000, 10, (LocationListener) Local);
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2 * 20 * 1000, 10, (LocationListener) Local);
    }

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == 1000) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            FuntionGPS();
        }
    }
}

public void setLocation(android.location.Location loc) {
    //Obtener la direccion de la calle a partir de la latitud y la longitud
    if (loc.getLatitude() != 0.0 && loc.getLongitude() != 0.0) {
        try {
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            List<Address> list = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
            if (!list.isEmpty()) {
                final Address DirCalle = list.get(0);
                DirCalle.getAddressLine(0);
                Call<com.model.Location> local = SmartApiAdapter.getApiService().Location(loc.getLatitude(),loc.getLongitude(),DirCalle.getAddressLine(0));
                local.enqueue(new Callback<Location>() {
                    @Override
                    public void onResponse(Call<Location> call, Response<Location> response) {
                        Toast.makeText(getBaseContext(),"Ubicacion actualizada", Toast.LENGTH_LONG).show();
                    }
                    @Override
                    public void onFailure(Call<Location> call, Throwable t) {
                        Toast.makeText(getBaseContext(),"Ultima ubicacion conocida: \n"+DirCalle.getAddressLine(0),Toast.LENGTH_LONG).show();
                    }
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class Localizacion implements LocationListener {

    MainActivity gpsActivity;

    public MainActivity getGpsActivity() {
        return gpsActivity;
    }
    void setGPSActivity(MainActivity gpsActivity) {
        this.gpsActivity = gpsActivity;
    }
    @Override
    public void onLocationChanged(android.location.Location loc) {
        loc.getLatitude();
        loc.getLongitude();
        timer1();
        this.gpsActivity.setLocation(loc);
        }
    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(getBaseContext(), "GPS desactivado", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(getBaseContext(),"GPS Activado", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        switch (status) {
            case LocationProvider.AVAILABLE:
                Log.d("debug", "LocationProvider.AVAILABLE");
                break;
            case LocationProvider.OUT_OF_SERVICE:
                Log.d("debug", "LocationProvider.OUT_OF_SERVICE");
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                Log.d("debug", "LocationProvider.TEMPORARILY_UNAVAILABLE");
                break;
        }
    }

}
    
asked by David Villegas 10.07.2018 в 22:22
source

1 answer

2

If you are using GPS only as provider,

locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

If you deactivate the GPS you could not really get information related to the geo location.

To reduce battery consumption, in this case you can use the NETWORK_PROVIDER and if it does not exist use GPS_PROVIDER since the GPS_PROVIDER consumes more battery because it involves hardware usage.

 boolean gpsEnabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
 boolean netEnabled = mlocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if(netEnabled){
            mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 20 * 1000, 10, (LocationListener) Local);
    }else if(gpsEnabled){
            mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2 * 20 * 1000, 10, (LocationListener) Local);
    }

There is an API that optimizes data collection and is recommended, it is called FusedLocationProviderClient

Here you find an excellent example .

    
answered by 10.07.2018 / 22:43
source