I have this Activity want to get current location but didn't now how to complete it in right way..also what is REQUEST_CODE_ASK_PERMISSIONS?? do I have to generate one?
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private double longitude;
private double latitude;
private TextView latitudeText, longitudeText;
private FusedLocationProviderClient fusedLocationProviderClient;
private GoogleApiClient googleApiClient;
private LocationManager locationManager;
private LocationListener locationListener;
private LocationRequest locationRequest;
private LocationCallback locationCallback;
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitudeText = findViewById(R.id.latitudeText);
longitudeText = findViewById(R.id.longitudeText);
getLocation();
}
private void getLocation() {
locationRequest = new LocationRequest()
.setInterval(2000).setFastestInterval(2000).setPriority(1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
latitude = location.getLatitude();
longitude = location.getLongitude();
latitudeText.setText(String.valueOf(latitude));
longitudeText.setText(String.valueOf(longitude));
}
}
};
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
}
});
}
@Override
protected void onResume() {
super.onResume();
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest
.permission.ACCESS_FINE_LOCATION},REQUEST_CODE_ASK_PERMISSIONS);
} else if(){
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
.....................................................................................................................................................
You should override
onRequestPermissionsResult
So it's up to you to define
REQUEST_CODE_ASK_PERMISSIONS
and it's role is to handle the callback, especially if you ask for more than on permission.And I think your
getLocation()
uses a deprecated API. Have a look here on how to get the location.Add dependency in build.gradle (Module:app)
Declare Necessary Permission in Manifest
EasyLocationProvider.java
Use:
Remove Location Update Callback
NOTE: Grant Permissions for Marshmallow and Above Version Devices
Ketan Ramani's answer helped me and all credits to him. I made a little adjustment to be able to set the number of updates especially in the event one is only required.
}
Setting the locationProvider