I'm developing an android app and I want to retrieve user's current location as soon as the app is launched.
I'm using this code:
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_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.
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
currentLatDouble = mLastLocation.getLatitude();
currentLngDouble = mLastLocation.getLongitude();
}
}
I am using a Snackbar
which pops up when the location is not detected and has a "RETRY" button. On pressing that "RETRY" button, the above code is executed again and this time the location is retrieved.
What I want is, I want to retrieve the location as soon as the app is launched.
Please let me know.