In the Neurolab-Android app, we have a feature for recording data. It uses data incoming from the hardware device and stores it in a data table format with various parameters. Two of these parameters happened to be the latitude and longitude (location) of the user using the app. For that, we needed to implement a location tracking feature which can be used while recording the data in the data table.
Let’s start off with adding the required permission uses in the Android Manifest file.
These will be used to ask permissions from the user to access the devices’ internet and location/GPS.
Now, we will be making our Location Tracker class that will be used in tracking the location and getting the corresponding latitude and longitude.
Firstly, we are going to define some variables – an array of manifest permissions for enabling GPS, some constant values for requesting specific permissions, a provider for the GPS provider.
We also need to initialize and have a location manager ready to request location updates from time to time. Defining a locationManager need to get the system service for location. We define it in the following way:
Next, we set up a location listener which listens to location changes of the device. We define that in the following way:
Now, that we have our variables, permissions, location manager and listener set up, we can start capturing the location.
Firstly, we need to check for the runtime permissions required for our task. We achieve this with the function ‘checkRuntimePermissions’ which we have created in a utility class named ‘PermissionUtils’. The code of this utility class can be found here: PermissionUtils.java. It basically self checks individual permissions from the array passed in the arguments with the Android system.
We then use the location manager instance to request current location updates using the constants and the location listener we defined earlier.
So now, that we have started capturing the user device location, we can get the device Location object values (latitude and longitude).
This method requests the location and returns a Location object. If there is an internet connection problem or the device location is disabled from the device settings, it returns a default location object. Here in the Neurolab project, we had set the default location latitude and longitude to 0.0.
Now we can use this location while creating the recorded file in our app directory which can be used after importing that recorded file in the app. We will be storing the location in two columns in the recorded file which is in a table format.
We write the latitude and longitude of the device in the file using a PrintWriter object and get the latitude and longitude of the device in the following way:
Then, using the PrintWriter object we can write the data into the file in the following way:
Here, the ‘data’ contains the latitude and longitude converted to String type.
That’s it! Now you can use the LocationTracker object to capture the location and get the current device location using it in your own app as well.
Hope this blog, adds value to your Android development skills.
Tags: FOSSASIA, Android, GPS, GSOC 19, Neurolab, Location