Updated 11 October 2021
As the new changes are being done for the better privacy of the user Android has updated the permission policy for location access. Android restricts its usage, especially in the background.
Before Android 9, there was no separate location permission for the Foreground and background apps therefore they were using the same resources due to this some of the apps were exploiting the location data of the users.
On Android 11 and higher, whenever your app requests access to the foreground location, the system permissions dialog includes an option called Only this time. This option gives users more control over when an app can access location information.
There are now two options available for the developer to pick from:
1: Foreground Location.
2: Background Location.
When you are developing an app that requires only that share or receives location information only one time, or for a specific amount of time then, that feature requires foreground location access.
To achieve this, we need to add the uses permission tag in the Android Manifest file.
1 2 |
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> |
From Android 11, the users now have 3 options to give access to their location:
1: While using the app
2: Only this time
3: Deny
To know the permission added on the Android manifest file are:
ACCESS_COARSE_LOCATION: Provides an estimate of the device location, within about 1 mile (1.6 km).
ACCESS_FINE_LOCATION: Provides an estimate of the device’s location as accurately as possible, usually within about 160 feet (50 meters) and sometimes as accurate as within 10 feet (a few meters) or better.
To get the current location using the foreground location with the help of an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
class MainActivity : AppCompatActivity() { lateinit var mFusedLocationClient: FusedLocationProviderClient lateinit var mLocationRequest: LocationRequest private val requestMultiplePermissions = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions -> var granted: Boolean = true permissions.entries.forEach { if (!it.value) { granted = false } } if (granted) { createLocationRequest() } else { Toast.makeText(this, getString(R.string.permission_denied), Toast.LENGTH_LONG) .show() } } private lateinit var mContentViewBinding: ActivityMainBinding public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) mContentViewBinding = ActivityMainBinding.inflate(layoutInflater) setContentView(mContentViewBinding.root) mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this) mContentViewBinding.currentLocation.setOnClickListener { requestMultiplePermissions.launch( arrayOf( Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION ) ) val task= mFusedLocationClient.lastLocation task.addOnSuccessListener { locationUI(it) } } } private fun createLocationRequest() { mLocationRequest = LocationRequest.create() mLocationRequest.interval = UPDATE_INTERVAL_IN_MILLISECOND mLocationRequest.fastestInterval = FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS mLocationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY } private fun locationUI(location: Location) { mContentViewBinding.foregroundLatLongTv.setText( getString(R.string.current_location)+ " == Latitude: "+ location.latitude.toString()+" and Longitude: "+location.longitude.toString()) } companion object { private const val UPDATE_INTERVAL_IN_MILLISECONDS: Long = 10000 private const val FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2 } } |
An app requires background location access if a feature within the app constantly shares a location with other users or uses the Geofencing API. Like sharing your live location.
From Android 10 (API level 29) and higher API level, you must have to declare the ACCESS_BACKGROUND_LOCATION permission in your app’s manifest to request background location access at runtime, otherwise, the system will ignore it. On earlier versions of Android, when your app receives foreground location access, it automatically receives background location access as well.
The second one is that the application needs to use location also in the background. For which you have to create a dialog with a precise message to explain the use of background location.
After the user agrees, redirect to the application’s setting page where you can choose the permission type as given below.
1: Allow all the time.
2: Allow only while using the app.
3: Ask every time
4: Deny
Now how to ask for the background location permission in code here is the below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@RequiresApi(Build.VERSION_CODES.Q) private fun backgroundLocationPermission(backgroundLocationRequestCode: Int) { if (checkPermissionGranted(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) return AlertDialog.Builder(this) .setTitle(R.string.background_permission_title) .setMessage(R.string.background_permission) .setPositiveButton(R.string.yes) { _,_ -> // this request will take user to Application's Setting page requestPermissions(arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION), backgroundLocationRequestCode) } .setNegativeButton(R.string.cancel) { dialog,_ -> dialog.dismiss() } .create() .show() } private fun checkPermissionGranted(permission: String) : Boolean { return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED } |
You can call the backgroundLocationPermission() method on the button click if you want, like the below code.
1 2 3 4 5 |
mContentViewBinding.backgroundLocation.setOnClickListener { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { backgroundLocationPermission(222) } } |
I have used a simple layout design.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="14dp" android:text="@string/foreground_location_permission" android:textColor="@color/black" android:textSize="18sp" android:textStyle="bold" /> <Button android:id="@+id/current_location" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="14dp" android:gravity="center" android:padding="14dp" android:text="@string/current_location" /> <TextView android:id="@+id/foreground_lat_long_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="14dp" android:gravity="center" android:textAlignment="center" android:textColor="@color/black" android:textSize="18sp" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="14dp" android:text="@string/background_location_permission" android:textColor="@color/black" android:textSize="18sp" android:textStyle="bold" /> <Button android:id="@+id/background_location" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="14dp" android:gravity="center" android:padding="14dp" android:text="@string/background_location" /> |
You can also check out the official documentation provided by Google click here.
. . . . . . . . .
That’s it from my side for today, thanks for reading it until now. You can check our other android blogs Click Here.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.