Understanding and Implementing Location Permission in Android Q

Understanding and Implementing Permission in Android Q

With Android Q releasing in Beta , there are a lot of new things coming to the Android Eco-System with it.

Privacy was the main focus with the Andorid Q release and with better handling of permissions, Background use, UI etc. A new permission was also introduced Activity Recognition Permission.But, In this blog we will be specifically talking about Location Permission in Android Q.

History about Location Services :

Location Services has evolved and has matured since the early days.At first we could use background services and take location updates "n" times a day but in coming years keeping privacy in focus developer can only take updates a few time only.

Since Android Oreo, it was recommended to take location updates using Foreground Services as it had no limit like background services and user can also see the updates coming from the notification tray.

This can be done using adding foregroundServiceType in the manifest file,

<service
    android:name="ForegroundServiceName"
    android:foregroundServiceType="location"/>

But, to make sure location works, we need to add ACCESS_COARSE_LOCATION in the project.

But in Android Q,

The whole world changed for Location Services in Q. Now, user had a lot of control on what to give as permission and what not too. Also user can permit appto get location updates in background as well. Let's discuss the it in step by step,

Step 01 :

<manifest>
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
</manifest>

Here, in the Manifest we don't need to specifically add the foregroundServiceType in manifest file.

Using Background Location with Access Coarse Location we can get 3 options for users to select from,

  1. Allow.
  2. Allow when app in Use.
  3. Deny.

Step 02 :

in your build.gradle of your app,

minSdkVersion 21
targetSdkVersion 'Q'
versionCode 1
versionName "1.0"

In the above code, you can see targetSdkVersion has been set to Q

Step 03 :

Let's ask the permission in Our Activity file,

private fun requestPermission() {
    val hasForegroundPermission = ActivityCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED

    if (hasForegroundPermission) {
        val hasBackgroundPermission = ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED
        if (hasBackgroundPermission) {
            // do your work here
        } else {
            ActivityCompat.requestPermissions(this,
                arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION), REQUEST_CODE_BACKGROUND)
        }
    } else {
        ActivityCompat.requestPermissions(this,
            arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_BACKGROUND_LOCATION), REQUEST_CODE_BACKGROUND)
    }
}

Here , in the above code we can only permit the background location if and only the foreground location is pemitted.

Now, lets run the app and we will see the following pop-up with a new re-design of material component

Understanding and Implementing Permission in Android Q

and if we click Deny here,

Understanding and Implementing Permission in Android Q

To read more about the Permissions in Andorid Q, Click here .