Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Location Permissions from User in Android Application [duplicate]

I´m trying to get the users actual location with the typicaly permissions box: like this: enter image description here

If anyone knows how to make it , pleas answer.

like image 456
Antonio Gschossmann Avatar asked Jun 05 '17 13:06

Antonio Gschossmann


People also ask

How can I get location from background in current version of Android?

In your app's manifest, check for the ACCESS_COARSE_LOCATION permission and the ACCESS_FINE_LOCATION permission. Verify that your app requires these location permissions. If your app targets Android 10 (API level 29) or higher, also check for the ACCESS_BACKGROUND_LOCATION permission.

What is shouldShowRequestPermissionRationale?

As per the documentation, shouldShowRequestPermissionRationale return the boolean indicating whether >or not we should show UI with rationale for requesting a permission.


1 Answers

Ask for permission at runtime to use device current location as below :

if (ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(YourActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
      return;
}else{
  // Write you code here if permission already given.
} 
like image 73
Haresh Chhelana Avatar answered Sep 24 '22 13:09

Haresh Chhelana