Turn on GPS automatically on Android (Programmatically)


Turn on GPS automatically on Android (Programmatically)

Turn on GPS automatically? What is that? Or we have seen it in different apps like Uber, Ola etc. but how to implement that?

We have seen various mobile applications want us GPS to turn on. And we proceed. First we select YES from dialog asking ‘You need to turn on GPS to continue’ or something like that (typical traditional alert dialog). Then it moves to the common GPS screen, showing list of other apps accessing GPS. However, next we toggle the location switch. And finally the back button. It returns to the application and wait for the location to be fetched and blah blah blah…

Tired enough! right?

Though it takes a few seconds(typically 4-5 seconds), still it feels like a huge task we are doing and of course, we don’t like it (at least some of us). It diverts our view from the app for a little but significant slice of time.

So we want to be a little bit lazy, right? This is where this article stands in , ‘How we can turn GPS automatically on Android’. Our goal is skip all of the steps written above and only one tap to switch on the GPS automatically.

ICE BREAK

We are assuming, you are accustomed with creating and running android applications. (If not, you can read this well written article). So let’s concentrate in main stream of discussion.

We will use Google Play Location Service to achieve our goal.

First of all, we need to set our AndroidMainfest.XML. We need permissions for INTERNET, ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION

. So our manifest will look like

Next, we will set up our Gradle files. Go to your build.gradle(app level) and add the following dependencies

That’s all before writing the main code to turn on GPS automatically in android. Next we will create our MainActivity where all our implementation will reside.

We will see the full source code at the end of the discussion. But before that let me explain you the whole code step by step.

Main Story

Starting with the design, as you can see below we have taken 3 text views in our activity_main.xml. One for header and other two for outputs to display.

Now, in our MainActivity.java we will implement 3 interfaces GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener and LocationListener. These are needed for listening connections and locations as they provide different methods to implement. We can do whatever we need when the listeners are called.

So our onCreate() method will look like

In the above code we can see we have attached our activity_main.xml as MainActivity’s screen and initialized the TextViews. Then we called setUpGClient(). So let’s take a look on setUpGClient()

Here we are initializing GoogleApiClient and calling it’s connect() method to access the APIs and it’s services.

LISTENERS & CALLBACKS

Okay, we have initialized and connected GoogleApiClient. Now it’s time to implement all the listener’s interfaces.

GoogleApiClient’s Connection callbacks are

  • void onConnected(@android.support.annotation.Nullable android.os.Bundle bundle);
  • void onConnectionSuspended(int i);

And OnConnectionFailedListener of GoogleApiClient has one interface

  • void onConnectionFailed(@android.support.annotation.NonNull com.google.android.gms.common.ConnectionResult connectionResult);

LocationListener has one callback

  • void onLocationChanged(android.location.Location location);

Now, we will override all the interfaces

Here we have called checkPermissions() method to check whether user have given permission for accessing locations(For Marshmallow and upper versions).

checkPermissions() method will look like

As you can see in the above code we requested permission for accessing FINE_LOCATION. And onRequestPermissionsResult() is the callback. Whenever permissions are granted or rejected this callback is fired. You can do whatever you want after getting the permission result. Here we called getMyLocation() method when permission is granted. And in checkPermissions() we checked if permission is given previously it directly calls the getMyLocation() method

We will discuss the getMyLocation() method later but before that let’s just finih the callbacks.

Next callback is onConnectionFailed() for GoogleApiClient.OnConnectionFailedListener . It is fired when connection gets failed due to internet connection or ther reasons. Let’s have a look

LocationListener‘s callback onLocationChanged() is fired whenever device gets location via GoogleApiClient. Here we will receive the location and display it to the front-end. It changes time to time as the device moves from one place to another.

REQUESTING LOCATION

We have called getMyLocation() from different portions. It’s time to see what it actually meant for. Let’s see the code first.

In getMyLocation() first we have checked if location permission is granted for this app. Next we created our LocationRequest instance and set request interval of 3 seconds. After that we requested Location Updates using FusedLocationApi.

Soon after, we are checking location settings using PendingResult and asking for location if GPS not activated(case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:…) by calling startResolutionForResult() (See the picture below). If GPS is enabled(case LocationSettingsStatusCodes.SUCCESS:…), we are getting last known location and initializing our mylocation variable. Then the onLocationChanged(Location location) is called and front-end gets updated instantly.

anroid GPS automatically

Full source code to Turn on GPS automatically

We have discussed all the portions we require to turn on GPS automatically from android. Below is the full source code.

Hope you liked this article. Feel free to comment for any convenience. We would like to hear from you. Please share it to inspire us.


Author
Abhishek is a developer cum blogger working more than 4 years. He loves programming especially open stack technologies. He has decent knowledge in Android development, Wordpress, MongoDB, Node.js and so on. Beside this Abhishek finds himself busy in painting, front-end designing.

Follow Abhishek


Previous


FCM Node Downstream Messaging
FCM downstream messaging using Node.js

Next


Android RecyclerView
Android RecyclerView with example