E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.cadastroDeAluno, PID: 17614
java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at com.google.android.gms.location.internal.zzi$zza$zza.zza(Unknown Source)
at com.google.android.gms.location.internal.zzk.zza(Unknown Source)
at com.google.android.gms.location.internal.zzl.zza(Unknown Source)
at com.google.android.gms.location.internal.zzd$1.zza(Unknown Source)
at com.google.android.gms.location.internal.zzd$1.zza(Unknown Source)
at com.google.android.gms.internal.zzpr$zza.zzb(Unknown Source)
at com.google.android.gms.internal.zzqa.zzf(Unknown Source)
at com.google.android.gms.internal.zzqa.zzd(Unknown Source)
at com.google.android.gms.internal.zzqf.zzd(Unknown Source)
at com.google.android.gms.internal.zzqd.zzd(Unknown Source)
at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
at br.com.cadastroDeAluno.mapa.Localizador.onConnected(Localizador.java:43)
at com.google.android.gms.common.internal.zzl.zzo(Unknown Source)
at com.google.android.gms.internal.zzqd.zzm(Unknown Source)
at com.google.android.gms.internal.zzqb.zzapm(Unknown Source)
at com.google.android.gms.internal.zzqb.onConnected(Unknown Source)
at com.google.android.gms.internal.zzqf.onConnected(Unknown Source)
at com.google.android.gms.internal.zzpu.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzk$1.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzd$zzj.zzarz(Unknown Source)
at com.google.android.gms.common.internal.zzd$zza.zzc(Unknown Source)
at com.google.android.gms.common.internal.zzd$zza.zzx(Unknown Source)
at com.google.android.gms.common.internal.zzd$zze.zzasb(Unknown Source)
at com.google.android.gms.common.internal.zzd$zzd.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
D/NativeCrypto: ssl=0x7f7283c200 sslRead buf=0x7f6e682800 len=2048,timeo=2500
W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:2
I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 2
D/GoogleCertificates: com.google.android.gms.googlecertificates module is loaded
D/GoogleCertificatesImpl: Fetched 190 Google release certificates
D/GoogleCertificatesImpl: Fetched 363 Google certificates
package br.com.cadastroDeAluno;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import br.com.cadastroDeAluno.mapa.Localizador;
import br.com.cadastroDeAluno.mapa.MapaFragment;
public class MapaActivity extends FragmentActivity { //Fragment
private static final int REQUEST_PERMISSOES = 1;
MapaFragment mapaFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction tx = manager.beginTransaction();
mapaFragment = new MapaFragment();
tx.replace(R.id.frame_mapa, mapaFragment); //new SupportMapFragment();
tx.commit();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] permissoes = {android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.INTERNET, android.Manifest.permission.ACCESS_FINE_LOCATION};
requestPermissions(permissoes, REQUEST_PERMISSOES);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_PERMISSOES) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED &&
grantResults[1] == PackageManager.PERMISSION_GRANTED &&
grantResults[2] == PackageManager.PERMISSION_GRANTED) {
new Localizador(this, mapaFragment);
}
}
}
}
package br.com.cadastroDeAluno.mapa;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.model.LatLng;
/**
* Created by Leandro on 18/10/2016.
*/
//classe responsável por gerenciar a parte do GPS
//classe Localizador que centraliza o mapa de acordo com a posição do dispositivo utilizando o GPS.
public class Localizador implements GoogleApiClient.ConnectionCallbacks, com.google.android.gms.location.LocationListener { //LocationListener
private final GoogleApiClient client; //a classe GoogleApiClient serve para fazer a conexão dentro do celular
private final MapaFragment mapaFragment;
public Localizador (Context context, MapaFragment mapaFragment){
client = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API) //acessar o LocationsService
.addConnectionCallbacks(this)
.build();
client.connect();
this.mapaFragment = mapaFragment;
}
// metodo em que especificamos o que fazer ao conectar
@Override
public void onConnected(@Nullable Bundle bundle) {
LocationRequest request = new LocationRequest();
request.setSmallestDisplacement(50); //define o deslocamento minimo
request.setInterval(1000); //intervalo de envio da posição
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); //com isso teremos uma precisão muito alta
LocationServices.FusedLocationApi.requestLocationUpdates(client, request, this);
}
//metodo em que especificamos o que fazer quando a conexao for suspensa
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
LatLng coordenada = new LatLng(location.getLatitude(), location.getLongitude());
mapaFragment.centralizarEm(coordenada);
}
}