The LocationManager
API on Android seems like it's a bit of a pain to use for an application that only needs an occasional and rough approximation of the user's location.
The app I'm working on isn't really a location app per se, but it does need to get the user's location in order to display a list of nearby businesses. It doesn't need to worry about if the user is moving around or anything like that.
Here's what I'd like to do:
- Show the user a list of nearby locations.
- Preload the user's location so that by the time I need it in
Activity
X, it will be available. - I don't particularly care about accuracy or frequency of update. Just grabbing one location is sufficient as long as it's not way off. Maybe if I want to be fancy I'll update the location once every few mins or so, but it's not a huge priority.
- Work for any device as long as it has either a GPS or a Network Location provider.
It seems like it shouldn't be that hard, but it appears to me that I have to spin up two different location providers (GPS and NETWORK) and manage each's lifecycle. Not only that, but I have to duplicate the same code in multiple activities to satisfy #2. I've tried using getBestProvider()
in the past to cut the solution down to just using one location provider, but that seems to only give you the best "theoretical" provider rather than the provider that's actually going to give you the best results.
Is there a simpler way to accomplish this?
Use the below code, it will give the best provider available:
Even though the answer is already given here. I just wanted to share this to the world incase the come across such scenario.
My requirement was that i needed to get a user's current location within 30 to 35 seconds at max so here is the solution i made following Nirav Ranpara's Answer.
1. I made MyLocationManager.java class which handles all the GPS and Network stuff
2. I made an Interface (callback) OnLocationDetectectionListener.java in order to communicate the results back to the calling fragment or activity
3. Then i made an MainAppActivty.java Activity that implements
OnLocationDetectectionListener
interface and here is how i receive my location in it4. Add the following permissions to your manifest file
Hope this is helpful to others :)
Kotlin version of @Fedor Greate answer:
usage of class:
MyLocation
Class :From last more than one year, I was using the combination of GPS_PROVIDER and NETWORK_PROVIDER to get the current location and it was working good, but from last few months I'm getting location after a long delay, so I switched to latest API FusedLocationProviderClient and it is working pretty good.
Here is the class which I wrote to get current location by using FusedLocationProviderClient. In below code, I used a timer to wait for while to get the current location, I scheduled timer 15 seconds delay, you can change it according to you.
And called this from activity, here is the code
Add following entries in the AndroidManifest.xml
EDIT: Updated with the latest Location Service API from Google Play Services library (July 2014)
I would recommend you to use the new Location Service API, available from the Google Play Services library, which provides a more powerful, high-level framework that automates tasks such as location provider choice and power management. According to the official documentation: "... Location API make it easy for you to build location-aware applications, without needing to focus on the details of the underlying location technology. They also let you minimize power consumption by using all of the capabilities of the device hardware."
For further information visit: Making Your App Location-Aware
To see a full example using the latest Location Service API visit: Android LocationClient class is deprecated but used in documentation
Here's what I do:
mFinalLocation
).onDestroy()
method of service, stop listening for location updates for each of the provider.Below is the code for service. You can run it based upon frequency of location update you need.