Over a million developers have joined DZone.

Location Aware App in Android: UV Index

Learn how to leverage Google Play services to make your app location aware with this useful tutorial.

· Mobile Zone

As you may already know Google Play services is a set of services that extend Android app features by providing a new set of services like Google Map, Google plus, Location Service and more. This post focuses on using Google Play Location Services to make a 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 your location. To get the UV index the app will use the Openweathermap API.

The final result is shown below:
Android location aware app

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

dependencies {
..
 compile 'com.google.android.gms:play-services:8.4.0'
..
}


Now the library is ready and we can use it in developing our Android app. 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 a Google Play services client to invoke the services exposed by Google Play so that the app can retrieve the current location.

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

private void initGoogleClient() {
 googleClient = new GoogleApiClient.Builder(this)
 .addConnectionCallbacks(this)
 .addOnConnectionFailedListener(this)
 .addApi(LocationServices.API)
 .build();
}


On 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:

@Override
protected void onStop() {
 googleClient.disconnect();
 super.onStop();
}


and to reconnect the client when the app starts:

@Override
protected void onStart() {
 googleClient.connect();
 super.onStart();
}


There's just 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
 @Override
 public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
 showErrorMessage();
 return;
 }

 // Connection established
 @Override
 public void onConnected(@Nullable Bundle bundle) {
 getUVIndex();
 }


The Google Play services client is ready and  when the connection is ready the app retrieves the UV Index.

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

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>


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);
req.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(googleClient, req, this);


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

To get the UV Index from Openweathermap, it is necessary to send the current location expressed as latitude and longitude:

http://api.openweathermap.org/v3/uvi/{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 known 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 'com.android.support:appcompat-v7:23.2.1'
 compile 'com.android.support:design:23.2.1'
 compile 'com.google.android.gms:play-services:8.4.0'
 compile 'com.squareup.okhttp3:okhttp:3.2.0'
}


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

private void handleConnection(Location loc) {

 double lat = loc.getLatitude();
 double lon = loc.getLongitude();

 // Make HTTP request according to Openweathermap API
 String url = UV_URL + ((int) lat) + "," + ( (int) lon) + "/current.json?appid=" + APP_ID;
 System.out.println("URL ["+url+"]");
 Request request = new Request.Builder()
 .url(url)
 .build();
 httpClient.newCall(request).enqueue(new Callback() {
 @Override
 public void onFailure(Call call, IOException e) {
 // Handle failure in HTTP request
 }

 @Override
 public void onResponse(Call call, Response response) throws IOException {
 // Ok we have the response...parse it
 try {
 JSONObject obj = new JSONObject(response.body().string());
 final double uvIndex = obj.optDouble("data");
 System.out.println("UV Index ["+uvIndex+"]");
 JSONObject jsonLoc = obj.getJSONObject("location");
 final double cLon = jsonLoc.getDouble("longitude");
 final double cLat = jsonLoc.getDouble("latitude");

 Handler handler = new Handler(MainActivity.this.getMainLooper());
 handler.post(new Runnable() {
 @Override
 public void run() {
 // Handle UI Update
 }
 });
 }
 catch(JSONException jex) {
 jex.printStackTrace();
 }
 }
 });
 }

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.

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.

Source code available soon.

Topics:
android

Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}