Location Services in Android (2)- Requesting for location updates

Omolara Adejuwon
Larikraun’s Blog
Published in
1 min readDec 27, 2015

Hey. This post is based on the assumptions that you understood what went down in the previous post on GETTING LAST KNOWN LOCATION.
This post will describe how to get LOCATION UPDATES in your android application. To get updates at an INTERVAL, you only need to make few changes to the implementation in my previous post.
These are the following changes:
1. Change the code snippet in the onConnected() method.

@Override
public void onConnected(Bundle bundle) {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000);
LocationServices.FusedLocationApi.requestLocationUpdates(mGac, mLocationRequest, this);
}

setInterval() tells your app how frequent you want updates to be sent.
setPriority() just places a priority on the request.
Then you call the
requestLocationUpdates().

2. Make your activity implement LocationListener. This would require you to override the onLocationChanged() method

@Override
public void onLocationChanged(Location location) {
Toast.makeText(getApplicationContext(), "Location has changed", Toast.LENGTH_LONG).show();
String locationDetails = "Latitude: " + location.getLatitude() + "\nLongitude: " + location.getLongitude();
locationUpdate.setText(locationDetails); //or do something else with the data.
}

Do not forget to do every other thing explained in the previous post.

You can also get the full implementation of this here on github.

--

--

Omolara Adejuwon
Larikraun’s Blog

Code and everything in between. Android Engineer. Editor @proandroiddev