请告诉我这段代码有什么问题,因为我没有使用此代码获取位置。请告诉我这是正确的方式还是他们以更好的方式获取位置或每5分钟
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if (ActivityCompat.checkSelfPermission(this, android.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.
ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},ACCESS_FINE_LOCATION_CODE);
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 0, this);
location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
Log.e("getLatitude", String.valueOf(location.getLatitude()));
Log.e("getLongitude", String.valueOf(location.getLongitude()));
return location;
} else {
Toast.makeText(this, "LOCATION NOT FOUND1", Toast.LENGTH_SHORT).show();
return null;
}
}
if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
if (ActivityCompat.checkSelfPermission(this, android.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.
ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},ACCESS_FINE_LOCATION_CODE);
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 300000, 0, this);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
Log.e("getLatitudeNET", String.valueOf(location.getLatitude()));
Log.e("getLongitudeNET", String.valueOf(location.getLongitude()));
return location;
} else {
Toast.makeText(this, "LOCATION NOT FOUND", Toast.LENGTH_SHORT).show();
return null;
}
}
else {
Toast.makeText(this, "CONNECTION LOST", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
e.printStackTrace();
}