1
Hello, good afternoon! I am having problems with the return of my location in my code (it is also not asking permission to the user)
CODE OF ACTIVITY
public class GPSActivity extends AppCompatActivity implements LocationListener {
private TextView tv_gpsTexto;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps);
SetToolbar();
tv_gpsTexto = (TextView) findViewById(R.id.tv_gpsTexto);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//Teste para permissão
if ((ContextCompat.checkSelfPermission(GPSActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
&& (ContextCompat.checkSelfPermission(GPSActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)){
//Requisição da permissão no celular (você vai dar acesso)
ActivityCompat.requestPermissions(GPSActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 100);
//Envia a localização para a plataforma
Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
onLocationChanged(location);
return;
}
}
@Override
public void onLocationChanged(Location location) {
//Pegar Latitude e Longitude
Log.d("TagPermissao3", "O USUÁRIO MUDOU A LOCALIZACAO");
double longitude = location.getLongitude();
double latitude = location.getLatitude();
tv_gpsTexto.setText("Logitude: " + longitude + "\n" + "Latitude: " + latitude);
}
private void SetToolbar() {
TextView tite;
tite = (TextView) findViewById(R.id.tite);
tite.setTypeface(GetFont.BoldFont(this));
tite.setText("GPS");
ImageView reply = (ImageView) findViewById(R.id.reply);
reply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
And, I have declared the following permissions in the Manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Welcome Friend! tour by the site and see How to create a Minimum, Complete and Verifiable example! So make it easier for the community to help you! Thank you!
– Thiago Luiz Domacoski