Show Location Setting Dialog Using Google API Client - Androhub

Location Dialog Banner

Show Location Setting Dialog Using Google API Client

In Google Maps Android application and many more other applications you had seen that if your Location is disabled then a request location dialog is appearing that directly turn GPS on without redirecting you to Settings page.

Location Dialog
Request Location Dialog

To achieve this dialog implementation we have to know below things:

  1. Google API Client : GoogleApiClient is used with a variety of static methods. Some of these methods require that GoogleApiClient be connected, some will queue up calls before GoogleApiClient is connected; check the specific API documentation to determine whether you need to be connected.
  2. Location Request : LocationRequest objects are used to request a quality of service for location updates from the FusedLocationProviderApi.

Prerequisite:

  1. Marshmallow Permissions.
  2. Broadcast Receiver.

Example

In this tutorial, we are going to learn how to implement Request Location Dialog with the help of GoogleAPIClient and we will use Broadcast Receiver to check GPS status.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Open build.gradle and include this libraries show below:

3. Open AndroidManifest.xml and include below permission to it for accessing location.

4. Create activity_main.xml and add the following code.

5. Now follow below steps to implement the functionality.

Step 1: Initiate Google API Client

Step 2: Now check Location Permission for Marshmallow Devices

Step 3: Method to show Location Dialog when GPS is Off

Step 4: Getting Result on user action over Location Request Dialog (Yes or No)

Step 5: Create a Broadcast Receiver to check the status of GPS every time whenever its get On and Off

Step 6: Show Location Request Dialog when GPS status Off. Here we have to use Handler to run call the method in Main Thread.

Step 7: Register and Unregister Receiver on onResume and onDestroy method respectively.

6. Finally open your MainActivity.java and add the following code to it.

7. Finally, all done run your code.

Thanks. 🙂

 

3 Comments

Kyrylo
Monday, July 10th, 2017

Thank you very much for this great tutorial! You’ve saved a lot of my nerves! Perfect!

Vivek Solanki
Friday, September 1st, 2017

Thank you , Buddy! It helped a lot ^_^

Shahnawaz
Saturday, November 21st, 2020

Tutorial was Great and worked.

Post comment

Your email address will not be published. Required fields are marked *