android location aware app

This post describes how to build an Android location-aware app, using Google play services. As you may already know Google play services is a set of services that extend the Android app features providing a set of new services like Google Map, Google plus, Location Service and more. This post focuses on using Google play location services to make location aware app. To show how to make it and how to set up an Android app based on Google play services, we will develop an app that shows the current UV index using the current location. To get the UV index the app will use the Openweathermap API.
The final result is shown below:
Android location aware app


Google play service setup

The first thing is setting up the Google play location service in build.gradle:

dependencies {
  compile '<latest version>'

Now the library is ready and we can use it in developing our Android app.
As you told before, the Android UV Index app should be aware of the current location so that it can pass the latitude and longitude to the Openweathermap API to get the current UV Index.
We have to develop then a Google play services client to invoke the services exposed by Google play so that the app can retrieve the current location.

Google play location service client

Making the client is very simple and we need just a few lines of code:

private void initGoogleClient() {
     googleClient = new GoogleApiClient.Builder(this)

At line 5, we specify we use LocationServices.API. The initGoogleClient is called in onCreate method so that we initialise the Google play location services client as soon as the app starts.
It is important to remember to disconnect the client to the services when the app stops:

protected void onStop() {

and to reconnect the client when the app starts:

protected void onStart() {

One more thing before the Google play services is ready, it is necessary to register the app to listen when the connection fails or the connection is ready:

// Connection failure
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

// Connection established
public void onConnected(@Nullable Bundle bundle) {

The Goolge play services client is ready and as you can notice when the connection is ready the app retrieves the UV Index.

Build location aware app

The next step is making an Android location aware app, so that the app can retrieve current UV Index.
Before implementing the app, it is necessary to grant the permission in the Manifest.xml:

To make the app location aware, we can ask the last known location using:

private Location getLocation() {
  try {
    Location loc = LocationServices.FusedLocationApi.getLastLocation(googleClient);
    return loc;
  catch(SecurityException se) {}
    return null;

where googleClient is the client we talked about in the previous paragraph.
This method can return null value so the app can register itself for location updates so that it gets informed when the location changes:

LocationRequest req = new LocationRequest();
req.setInterval(60 * 60 * 1000);
LocationServices.FusedLocationApi.requestLocationUpdates(googleClient, req, this);

where the interval is the notification interval. To know more about LocationRequest refer to the official documentation.

Openweathermap UV Index API

To get the UV Index from Openweathermap, it is necessary to send the current location expressed as latitude and longitude:{lat},{lon}/current.json?appid={your-api-key}

and of course, the API-key. You can get it creating an account. If you want to have more information go to how to invoke Openweathermap API in Android.
To invoke the Openweathermap API is very simple once we know the current location (latitude and longitude).
The Android app location-aware has to make an HTTP call to the Openweathermap API and parse the JSON response. As HTTP client library, the app uses OkHttp library, so the build.gradle is:

dependencies {
  compile '<latest version>'
  compile '<latest version>'
  compile '<latest version>'
  compile 'com.squareup.okhttp3:okhttp:<latest version>'

Once the dependency is configured to make an HTTP request is very simple:

The final result is shown in the picture below:
Android location aware app

The color of the value shown in the image above changes according to UV Index scale.

Test Location aware app

To test the app, it is necessary to use a simple Android app that returns fake GPS location and enables fake GPS position in your smartphone under the developer section in the configuration menu.




Please enter your comment!
Please enter your name here