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