Hello everyone,
Today I would like to show you how to use the LocationManager classes to collect the exact location of your mobile during the execution of your application. As part of the content, I would like to talk a little bit about sensors as well. Please note: there is one video published on
youtube that is related of this article that I wrote, find below more details below:
Summary:
The Use LocationManager object using getSystemService(Context.LOCATION_SERVICE), then instantiate LocationListenet , and this listener needs to override some methods, such as OnLocationEnableb, OnStatusChanged or OnProviderEnable and Disable.
How to get Location Updates:
1-Call requestLocationUpdates(provider, minTime,minDistance, Listener)
2-Call removeUpdates(Listener) to stop getting Location.
It works the same way that you call some other methods, as for example the Hardware Sensors. But a very important information here is:
Any service or activity that will ask for a listener on sensor data should implement the SensorEventListener interface.
The java class SensorEventListener has two required methods:
onAccuracyChanged() -> when accuracy of the sensor changes
onSensorChanged() -> when sensor reports new values.
So, when the sensor values change on mobile system, the values array(in the event that goes using onSensorChanged method) will change.
The Event Values:
The value here is an array of values returned from the sensor.
Each type of sensor has a different array of values, such as X,y,z axis(math).TYPE_proximity has only one element in its values array(eg: values[0]).
Warning: Preserve the battery! When you are dealing with location, the hardware that works for sensor is highly consumed, per user action, delays parameters
and others.
Example of delays parameters:
SENSOR_DELAY_NORMAL: sampling rate for screen orientation changes.
SENSOR_DELAY_UI: For monitoring changes that will be applied to the user(interface).
SENSOR_DELAY_FASTEST: a zero microsecond delay.
SENSOR_DELAY_GAME: for monitoring changes for a use in a typical game.
Saying that, let's go to the main coding of this article:
1) Creating your android project you will see the 3 main files we are going to work with:
LocationThiagoLeoncioActivity.java, activity_location_thiago_leoncio.xml and AndroidManifest.xml
1.1) On activity_location_thiago_leoncio.xml I will drag and drop all TextViews that I am going to use as a display.
1.2)On LocationThiagoLeoncioActivity.java we will write our methods that are going to be displayed(called) by the main onCreate method.
In this case is calling a method setupLocation() that is provided below, and the embedded method showLocationofThiago():
1.3) Last but not least the AndroidManifest.xml that allows the mobile emulator user to get the location:
2) The way that I developed when you run your emulator and launch your Android Device Monitor sending the location it will show you something similar to this below:
To help you better understand this, please watch this video that I've published: https://youtu.be/9Q0w71dUsys
I hope it helps you,
Thiago Leoncio.