0
I’m doing a test using Google Maps. It’s working but it’s like this. As soon as the app starts, it checks the permission by calling the Permissao class. If the user accepts, the app starts and if they refuse to close the app with a message stating that you must accept the permissions. So far so good, but if it accepts the permission the app only works if it closes and opens again the application or if it rotates the screen.
Basically the app takes the current location of the device and shows it on the map. I wish once he chose to allow, the app would already work without the details I mentioned above.
private GoogleMap mMap;
private Marker marcador;
double lat = 0.0;
double lng = 0.0;
private String[] permissoesNecessarias = new String[]{
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
Permissao.validaPermissoes(1, this, permissoesNecessarias);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
minhaLocalizacao();
}
private void adicionarMarcador(double lat, double lng) {
LatLng coordenadas = new LatLng(lat, lng);
CameraUpdate minhaLocalizacao = CameraUpdateFactory.newLatLngZoom(coordenadas, 16);
if (marcador != null) marcador.remove();
marcador = mMap.addMarker(new MarkerOptions()
.position(coordenadas)
.title("Você está aqui")
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_round)));
mMap.animateCamera(minhaLocalizacao);
}
private void atualizarLocalizacao(Location location) {
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
adicionarMarcador(lat, lng);
}
}
LocationListener locListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
atualizarLocalizacao(location);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
private void minhaLocalizacao() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
}
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
atualizarLocalizacao(location);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, locListener);
}
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
for ( int resultado : grantResults){
if ( resultado == PackageManager.PERMISSION_DENIED){
alertaValidacaoPermissao();
}
}
}
private void alertaValidacaoPermissao() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Permissão");
builder.setMessage("Para usar o app você precisa aceitar as permissões ");
builder.setPositiveButton("Confirmar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
builder.create();
builder.show();
}
}
Putz was that .... Thanks, I just changed from there too in the method
minhalocalizacao
theif
equal to not . I tested here and it worked. Thank you– Flávio