Skcript
All ArticlesArrow SparrowArrow Sparrow

Get Location Permission on Android Q

— Written by

Google introduces the new Android OS called Android Q or 10. The Android 10 has released with a lot of new features like Dark Mode, Smart Reply, Focus Mode and so on. Android 10 has changed the way of handling the location permissions; the system includes multiple permissions related to location. And, then the location permissions fulfil the location requirements for your app’s use cases. In this article, we are gonna discuss and implement the new location permissions model in our android application.

Types of location access

Android Q location permissions have the following categories of location access:

  1. Foreground Location Access
  2. Background Location Access

Foreground Location Access

If your application shares or receives the location for a one-time or particular amount of time then that feature requires foreground location access. Some examples are added below,

  • Users get turn-by-turn directions in a navigation app.
  • Users can share their current location with another user in a messaging app.

You need to declare the permissions for foreground access in manifest either “ACCESS_COARSE_LOCATION” permission or the “ACCESS_FINE_LOCATION” permission, as shown in the below snippet:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

Background Location Access

When an app needs to share the location continuously with other users, it requires background location access. Some examples are included below:

  • In a family location sharing app, the feature allows users to continuously share location with family members.

The level of sureness depends on which permission you request:

  1. ACCESS_COARSE_LOCATION It provides an accurate location within the city block.
  2. ACCESS_FINE_LOCATION It provides a more accurate location than one provided when you request “ACCESS_COARSE_LOCATION”. This permission is necessary for some other connectivity tasks like connecting to nearby devices over Bluetooth Low Energy (BLE).

The COARSE_LOCATION will pop up a dialog with the three options when you access the location.

  1. Allow Always On selecting “Allow Always”, the location is continuously shared with the user all the time, whether the app is either foreground or background.
  2. Allow only while using the app It shares user location only when the application is used. It stops the location sharing after the user closes the application.
  3. Deny While clicking “Deny”, it disables the location permission request but when you look for the location request for a second time it shows the fourth option - Deny & Do Not Ask Again.

Code

Code for the activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">

   <Button
       android:id="@+id/LocationPermissionButton"
       android:layout_width="200dp"
       android:layout_height="40dp"
       android:text="LOCATION PERMISSION"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       />

</RelativeLayout>

Code inside MainActivity.java

package com.example.locationpermission;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

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

public class MainActivity extends AppCompatActivity {

   public static final int REQUEST_CODE_PERMISSIONS = 101;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       Button btnPermissions = findViewById(R.id.LocationPermissionButton);

       requestLocationPermission();

       btnPermissions.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View v) {
               requestLocationPermission();
           }
       });

   }

   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) {
               handleLocationUpdates();
           } else {
               ActivityCompat.requestPermissions(this,
                       new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, REQUEST_CODE_PERMISSIONS);
           }
       } else {
           ActivityCompat.requestPermissions(this,
                   new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                           Manifest.permission.ACCESS_BACKGROUND_LOCATION}, REQUEST_CODE_PERMISSIONS);
       }
   }

   @Override
   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();
                       continue;
                   } else {
                       Toast.makeText(getApplicationContext(), "Location Permission denied", Toast.LENGTH_SHORT).show();
                       break;
                   }
               }

               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) {
                   handleLocationUpdates();
               } else {
                   handleForegroundLocationUpdates();
               }
           }
       }
   }

   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();
   }
}

OnRequestPermissionResult check the location permission granted or not. On clicking the location permission button, it will ask you for foreground and background location access. If you do not select foreground access the loop will break. There is no use of background location access without giving a foreground location access.

I have attached the output of the above application below for reference.

Up next

Using VS Code to Internationalize Content
Skcript https://skcript.com/svr/get-location-permission-on-android-q/ https://skcript.com/svrmedia/heroes/get-location-permission-on-android-q-2x.jpg

We collaborate with ambitious brands and people.

Book a free consultationArrow SparrowArrow Sparrow