Good afternoon friends I have a problem, Well I have been doing tests and I would like to know where I am failing, as I see when I do debugeo, location returns null and therefore can not return the get Altitude or any other property, I would like to know the error or if it is not that of location What can be? if I have the permissions in manifest and puej I have an emulator nexus 5x api21 android 5.0.2
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.btnSitios_Turisticos = (Button) findViewById(R.id.btn_sitios_turistico);
this.btnSitios_Turisticos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intento_abrir_mapsactivity = new Intent(MainActivity.this, MapsActivity1.class);
startActivity(intento_abrir_mapsactivity);
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Toast.makeText(this
,"La latitud es:"+String.valueOf(location.getLatitude()),Toast.LENGTH_LONG).show();
}
}
Here I have been doing a test but location is null when I do the debugeo thank you very much for the help.