Android 10 – Location Permissions

Filed Under: Android

In this tutorial, we’ll be discussing and implementing the new location permissions model in our android application.

Note: Google has taken a detour from the Android Alphabetical versions. Android Q has been renamed to Android 10. Since this tutorial was written before Google decided to do this, you’ll see Android Q at some places in the article.

Android 10 Location Permissions

With the introduction of Android 10, besides the dialog UI, the way of handling location permissions has also changed.
Now the user is allowed to choose whether they want location updates when the app is in the background.
For that a new permission needs to be declared in the Manifest file:

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

Calling this along with COARSE_LOCATION would pop up a dialog with three options:

  • Always Allow
  • Allow only while using the app
  • Deny

On selecting Deny, the next time the dialog will show a fourth option – Deny & Do Not Ask Again.

Always Allow ensures that you can poll for location updates in foreground and background.

If you select “Allow only while using the app”, the next time the permission dialog will only ask you to always allow the location permission or deny.

In the following section, we’ll be implementing our first Android Q Application.
Let’s update the SDK Manager and also create a new AVD with Android Q.

Project Structure

Our build.gradle file is given below:

apply plugin: ''

android {
    compileSdkVersion 'android-Q'
    defaultConfig {
        applicationId "com.journaldev.androidqlocationpermissions"
        minSdkVersion 16
        targetSdkVersion 'Q'
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner ""
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), ''

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
    implementation ''
    testImplementation 'junit:junit:4.12'
    androidTestImplementation ''
    androidTestImplementation ''

We’ve upgraded the dependencies for Android – Q.


The code for the activity_main.xml layout is given below:

<?xml version="1.0" encoding="utf-8"?>
< xmlns:android=""

        android:text="LOCATION PERMISSION"
        app:layout_constraintTop_toTopOf="parent" />


Inside the :

package com.journaldev.androidqlocationpermissions;

import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;

public class MainActivity extends AppCompatActivity {

    Button btnPermissions;

    public static final int REQUEST_CODE_PERMISSIONS = 101;

    protected void onCreate(Bundle savedInstanceState) {

        btnPermissions = findViewById(;

        btnPermissions.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

    private void requestLocationPermission() {

        boolean foreground = ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;

        if (foreground) {
            boolean background = ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED;

            if (background) {
            } else {
                        new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, REQUEST_CODE_PERMISSIONS);
        } else {
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_BACKGROUND_LOCATION}, REQUEST_CODE_PERMISSIONS);

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_PERMISSIONS) {

            boolean foreground = false, background = false;

            for (int i = 0; i < permissions.length; i++) {
                if (permissions[i].equalsIgnoreCase(Manifest.permission.ACCESS_COARSE_LOCATION)) {
                    //foreground permission allowed
                    if (grantResults[i] >= 0) {
                        foreground = true;
                        Toast.makeText(getApplicationContext(), "Foreground location permission allowed", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "Location Permission denied", Toast.LENGTH_SHORT).show();

                if (permissions[i].equalsIgnoreCase(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {
                    if (grantResults[i] >= 0) {
                        foreground = true;
                        background = true;
                        Toast.makeText(getApplicationContext(), "Background location location permission allowed", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "Background location location permission denied", Toast.LENGTH_SHORT).show();


            if (foreground) {
                if (background) {
                } else {

    private void handleLocationUpdates() {
        //foreground and background
        Toast.makeText(getApplicationContext(),"Start Foreground and Background Location Updates",Toast.LENGTH_SHORT).show();

    private void handleForegroundLocationUpdates() {
        //handleForeground Location Updates
        Toast.makeText(getApplicationContext(),"Start foreground location updates",Toast.LENGTH_SHORT).show();

In the onRequestPermissionResult method, we check whether the permission is granted or not.
If the foreground location permission is not granted then we break out of the loop.
This is because, without the foreground permission allowed, the background location permission is of no use.

Once the permission is granted, you can poll for the location updates.

The output of the above application in action is given below:

Android Q Location Permissions Output 1

Android Q Location Permissions Output 1

We can go to the Settings | Apps | Permissions to view the permission is granted or not as shown in the illustration below:

Android Q Location Permissions Settings

Android Q Location Permissions Settings

Once the permission is denied and do not ask again is selected, the dialog would no longer open.

That brings an end to this tutorial. You can download the complete Android Q Location Permissions Project from the link given below or check out the Github Repository for the full source code.


  1. nebi says:

    10q very much this saves my time alot

  2. harsha says:

    If am selected as allow all the time am able to check is device location is enabled or not then after show location enable dialog.
    Same if am select allow while using app, not able to show dialog same for allow all the time. Please help mw if am select allow while using app can i get location access or not.

  3. Nana says:

    I tested this code in an emulator and is always getting location permission denied

  4. Nana says:

    This code does not work at all please test it again

  5. Ashish says:

    How to get lat, lng of location

    1. Anoop says:

      use LocationRequest for lat long

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors