Android Location APIs are used in a wide variety of apps for different purposes such as finding user location, notifying when a user has left a general area (Geofencing), and help interpret user activity (walking, running, driving, etc).
However, Android Location APIs are not the only means of acquiring user location. The following will give examples of how to use Android's LocationManager
and other common location libraries.
For building Location aware apps in Android, there are two paths:
LocationManager
FusedLocationProviderApi
, which is part of Google Play ServicesPros
Cons
Features
Providers
GPS
Network
Passive
Pros
Cons
Features
LocationRequest Priority Levels
PRIORITY_HIGH_ACCURACY
ACCESS_FINE_LOCATION
for more accurate location or ACCESS_COARSE_LOCATION
for less accurate locationACCESS_FINE_LOCATION
is not used, this will not use GPS for generating location updates, but will still find a fairly accurate point in the right conditions.ACCESS_FINE_LOCATION
is used, it may or may not use GPS to generate location points, depending on how accurate it can currently track the device given the environmental conditions.PRIORITY_BALANCED_POWER_ACCURACY
ACCESS_FINE_LOCATION
for more accurate location or ACCESS_COARSE_LOCATION
for less accurate locationPRIORITY_HIGH_ACCURACY
PRIORITY_LOW_POWER
PRIORITY_NO_POWER
LocationManager
PASSIVE_PROVIDER
PASSIVE_PROVIDER
reports back underlying location updates usedOnLocationChanged() Never Called
Since this seems to be a common issue with getting Android Locations, I'll put down a quick checklist of common fixes:
Check your manifest!
One of the most common issues is that the right permissions were never given. If you are using GPS (with or without Network), use <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
, else use <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
. Google's FusedLocationApi requires ACCESS_FINE_LOCATION
.
(For Android 6+) Check runtime permissions!
Check for and request permissions! If you are never given permissions, you'll end up with crashes, or worse (if you are catching all exceptions), you'll end up with no indication of anything! It doesn't matter if the user grants you permission at the start of the app, always check to see if you have permissions for all calls. The user can easily go to their settings and revoke them.
Double check your code!
Are you sure you are passing in the right listener? Did you add that BroadcastReceiver
or IntentService
to your manifest? Are you using PendingIntent.getService()
on a BroadcastReceiver
class, or getBroadcast()
on an IntentService
class? Are you sure you are not unregistering your listener somewhere else in your code immediately after requesting?
Check device settings!
Obviously, make sure you have location services turned on.
If you are using Network services, did you turn on "Scanning Always Available"? Do you have your location mode set to "Best" ("High Accuracy") or "Battery Saving" ("Network Only")?
If you are using GPS, did you turn on "Best" ("High Accuracy") or "Device only" in location mode?
Double check your code!
Yes, this is on here twice. Did you try using a LocationListener
instead of a PendingIntent
, or vice-versa, to ensure you actually implemented LocationManager
properly? Are you sure that the location request isn't being removed in some part of the Activity or Service lifecycle that you didn't expect to happen?
Check your surroundings!
Are you testing GPS on the first floor of a building in the middle of San Francisco? Are you testing Network locations in the middle of nowhere? Do you work in a secret underground bunker void of all radio signals, wondering why your device isn't getting location? Always double check your surroundings when trying to troubleshoot location problems!
There could be many other less obvious reasons why location isn't working, but before searching out those esoteric fixes, just run through this quick checklist.