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

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.

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

ConnectionFailed Callback

1

2

3

4

5

6

7

8

@Override

publicvoidonConnectionFailed(ConnectionResult connectionResult){

//You can display a message here

}

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.

onLocationChanged

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@Override

publicvoidonLocationChanged(Location location){

mylocation=location;

if(mylocation!=null){

Doublelatitude=mylocation.getLatitude();

Doublelongitude=mylocation.getLongitude();

latitudeTextView.setText("Latitude : "+latitude);

longitudeTextView.setText("Longitude : "+longitude);

//Or Do whatever you want with your location

}

}

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.

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.

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 downstream messaging using Node.js

Next

Featured Post

ExoPlayer is an Android Application level Media Player by Google. It is used for playing media(both audio and video) from local or remote. In this article we will discuss how to play or stream remo ... Read More