Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to Fetch and Request Updates on User’s Location in Android

With the help of this article, we will see the ideas that can be used in an application to fetch the exact and accurate user location details.

To determine the location of a user is not an easy task. You need to build a very smart application to accomplish this. Talking about the location finding application for Android, one can make use of GPS as well as Internet Service provider of the Android device so as to fetch the details of user location. GPS actually consumes a lot of battery power therefore it is recommended to make use of the Service Provider Network that identifies user location with cell tower and Wi-Fi signals. This provides the location details in such a way that is intended to perform both indoors as well as outdoors. The most important thing is it consumes less battery power than GPS.

Issues in Identifying Location of the User

It is very complex to determine the location of a user from a mobile device. Let us look at the reasons for the error in location reading that could arise sometimes.

  • One can get confuse to use the relevant source such as GPS or Wi-Fi can to get the user location and to determine the same is a key challenge.
  • The user whom you are tracking may be changing the location very frequently so it requires a re-estimation quite often
  • Estimates retrieval from each of the user location may not be accurate as always. The newer location may be accurate or irrelevant making use of any source.

The issues listed above may be cause a difficulty to fetch a reliable reading of the user location. Let us see how to overcome the same.

How to Request Updates for User Location?

Let us see how we can fetch or retrieve the user location on Android devices. Getting the location of the user happens by the route of a callback method. requestLocationUpdates() can be used to get the updates from LocationManager and passing it a LocationListener that must be impleneted for lot number of callback methods that is called by the LocationManager at the time when the location of the user is changed or the change in the service status.

The below code displays the method to identify a LocationListener and request location updates:

Listing1 : Determining Location Updates

// Fetch a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Identify a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
      makeUseOfNewLocation(location);
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  • The very first parameter in requestLocationUpdates() is defined as the sort of location provider that can be used which in this case is nothing but the Internet Service Provider and Wi-Fi based location.
  • The second and third parameter can be used to control the frequency at which your listener receives updates. The second one is the minimum time interval between notifications whereas the third one is the minimum change in distance between notifications
  • In order to receive the location updates, we have a last parameter i.e. LocationListener.
  • In order to make use of GPS to determine the location of the user, one can replace NETWORK_PROVIDER by GPS_PROVIDER.
  • Also, the location updates can be requested from both GPS as well Service Provider by making use of requestLocationUpdates() two times- one for NETWORK_PROVIDER and one for GPS_PROVIDER.

How to Request for Permissions for the User?

One is required to request user permission so as to receive location updates from NETWORK_PROVIDER or GPS_PROVIDER. This can be done by declaring either ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in the Android manifest file. The below code lists the example of the same:

Listing 2: Requesting User’s Permission

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

The application will not perform to the expectation at the run time when requesting for the location updates in case you do not make use of these user permissions.

The Model for Fetching the User’s Location

The below steps lists the model steps to obtain the user location:

  • Step1: Initiate the Application.
  • Step2: At few minutes, begin to listen for the location updates from the required location provider
  • Step3: Filter out the best and accurate of the location readings and maintain the best of the estimates.
  • Step4: Stop the application to get the latest updates for the user’s location.
  • Step5: Take the reading of the best and accurate estimations.

The Perfect time to initiate listening for updates

One might be eager to get the location updates as soon as the application is started. The other time could be when users activate a specific feature. You need to be aware that there can be a lot of battery consumption in case the application is just searching for the location of the user with no success in hand and on the other hand, the usage of the application for a shorter time may not give you an accurate result or outcome.

The code mentioned below makes use of requestLocationUpdates() to initiate listening for user location updates.

Listing3: Listening for Updates

LocationProvider locationProvider = LocationManager.NETWORK_PROVIDER;
// Or, use GPS location data:
// LocationProvider locationProvider = LocationManager.GPS_PROVIDER;

locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

Conclusion

We learned today how to get the user location details making use of the methods discussed above and the perfect time to get the updates on the same.



Software Developer from India. I hold Master in Computer Applications Degree and is well versed with programming languages such as Java, .Net, C and C++ and possess good working knowledge on Mobile Platforms as well.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login