Show Location Setting Dialog Using Google API Client

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. 🙂

Download Source Code – >

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)