I'm making an app that reads the GPS coordinates.
When the sensor is turned on and working, the app pulls the information right, but when the sensor is turned off, the app hangs when I try to read the coordinates
package com.example.dfabr.primeiroprograma;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.List;
public class telaInformacao extends AppCompatActivity implements LocationListener {
Button btnReceberCoordenadas;
TextView receberCoordenadas;
public TextView txtCidade;
public TextView txtEStado;
public TextView txtPais;
private Address endereco;
double latitude;
double longitude;
public static final int PREMISSAO =1;
Location location = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
getSupportActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tela_informacao);
btnReceberCoordenadas = (Button)findViewById(R.id.btnReceberCoordenadas);
receberCoordenadas = (TextView)findViewById(R.id.receberCoordenadas);
txtCidade = (TextView)findViewById(R.id.txtCidade);
txtEStado = (TextView)findViewById(R.id.textEstado);
txtPais = (TextView)findViewById(R.id.textPais);
btnReceberCoordenadas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(telaInformacao.this, "Suas Coordenadas Foram Geradas Com Sucesso", Toast.LENGTH_LONG).show();
lerCoordenadas();
}
});
}
private void lerCoordenadas(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PREMISSAO);
}else{
LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (gps){
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i("Coordenadas", "Latitude: \n" + latitude + "Longitude: \n " + longitude);
receberCoordenadas.setText("Latitude: " + latitude + "\n\nLongitude: " + longitude);
}
}else
Toast.makeText(telaInformacao.this, "GPS desativado", Toast.LENGTH_LONG).show();
try {
endereco = buscarEndereco(latitude,longitude);
txtCidade.setText("Bairro: " + endereco.getSubLocality());
txtEStado.setText("Estado: " + endereco.getAdminArea());
txtPais.setText("Pais: " + endereco.getCountryName());
}catch (IOException e){
Log.i("GPS", "GPS");
}
}
}
public Address buscarEndereco(double latitude, double longitude)
throws IOException {
Geocoder geocoder;
Address address = null;
List<Address> addresses;
geocoder = new Geocoder(getApplicationContext());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
address = addresses.get(0);
}
return address;
}
/*public void onRequestPermissionResult(int requestCode, @NonNull String[] permission, @NonNull int[] grantresult) {
switch (requestCode){
case PREMISSAO:
if (permission[0].equalsIgnoreCase(Manifest.permission.ACCESS_FINE_LOCATION) &&
grantresult [0] == PackageManager.PERMISSION_GRANTED)
lerCoordenadas();
}
super.onRequestPermissionsResult(requestCode, permission, grantresult);
}*/
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}