Location

suggest change

Versions

[{“Name”:“3.0.x”,“GroupName”:null},{“Name”:“3.1.x”,“GroupName”:null},{“Name”:“3.2.x”,“GroupName”:null},{“Name”:“4.0”,“GroupName”:null},{“Name”:“4.0.3”,“GroupName”:null},{“Name”:“4.1”,“GroupName”:null},{“Name”:“4.2”,“GroupName”:null},{“Name”:“4.3”,“GroupName”:null},{“Name”:“4.4”,“GroupName”:null},{“Name”:“4.4W”,“GroupName”:null},{“Name”:“5.0”,“GroupName”:null},{“Name”:“5.1”,“GroupName”:null},{“Name”:“6.0”,“GroupName”:null},{“Name”:“7.0”,“GroupName”:null},{“Name”:“7.1”,“GroupName”:null}]

Introduction

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.

Remarks

For building Location aware apps in Android, there are two paths:

LocationManager

Pros

Cons

Features

Providers

GPS

Network

Passive


FusedLocationProviderApi

Pros

Cons

Features

LocationRequest Priority Levels

PRIORITY_HIGH_ACCURACY

PRIORITY_BALANCED_POWER_ACCURACY

PRIORITY_LOW_POWER

PRIORITY_NO_POWER

TroubleShooting

OnLocationChanged() Never Called

Since this seems to be a common issue with getting Android Locations, I’ll put down a quick checklist of common fixes:


  1. 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.


  1. (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.


  1. 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?


  1. Check device settings!
Obviously, make sure you have location services turned on.

[![enter image description here][8]][8]

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")?

[![enter image description here][9]][9]

If you are using GPS, did you turn on "Best" ("High Accuracy") or "Device only" in location mode?

[![enter image description here][10]][10]

  1. 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?


  1. 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents