Location aware app in Android: UV Index

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:

Google play service setup

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:

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:

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:

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:

About The Author

I'm an electronic engineer with over 15 years of
experience in computer programming. I'm the author of the book "Android things projects". I'm a MVB at DZone and DZone Guide Author. I have contributed to the IoT guide. I'm the technical reviewer of the book Internet of Things with Arduino Blueprints(published by PacktPub). I love creating IoT projects using Arduino, Raspberry Pi,
Android, and other platforms. I am interested in the convergence
between IoT and the mobile applications. I'm SCEA, SCWCD, and SCJP
certified.
Follow on: Twitter | Linkedin

valdow

is it only getting data once a day at 12:00:00 ?
can i get something more precise for openweather free api key

Get more stuff

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.