If you haven’t set up Google Play services yet, please follow this tutorial.
First, update your AndroidManifest.xml file to request the permissions for accessing locations:
The two permissions allow you to control the accuracy of the requested locations, and you don’t have to request both for your app. If you only request the coarse location permission, the fetched location will be obfuscated. However, if you want to use the geofencing feature, you must request the ACCESS_FINE_LOCATION permission.
Connect Location Client
With the new GoogleApiClient class, you can connect all needed services at once, and Google Play services will handle all the permission requests, etc.:
Access Current Location
Once connected, you can easily fetch the current location:
Note that this getLastLocation() method might return null in case location is not available, though this happens very rarely. Also, it might return a location that is a bit old, so the client should check it manually.
Listen to Location Updates
Let’s extend the above code snippet:
The difference between setFastestInterval() and setInterval() is:
If the location updates is retrieved by other apps (or other location request in your app), your onLocationChanged() callback here won’t be called more frequently than the time set by setFastestInterval().
On the other hand, the location client will actively try to get location updates at the interval set by setInterval(), which has a direct impact on the power consumption of your app.
Then how about background location tracking? Do I need to implement a long-running Service myself? The answer is simply, no.
With this, your listener (it can be an IntentService, or a BroadcastReceiver) as defined in the PendingIntent will be triggered even if your app is killed by the system. The location updated will be sent with key FusedLocationProviderApi.KEY_LOCATION_CHANGED and a Location object as the value in the Intent:
With geofencing, your app can be notified when the device enters, stays in, or exits a defined area. Please note that geofencing requires ACCESS_FINE_LOCATION. Again, let’s keep extending the above sample:
Here, the loitering delay means the GEOFENCE_TRANSITION_DWELL will be notified 30 seconds after the device enters the area. There’re also limitations on the number of geofences (100 per app) and pending intents (5 per app) enforced.
When a geofence transition is triggered, you can find more details easily e.g. in an IntentService:
Finally, to remove geofences, you can simply use one of the overloaded LocationServices.GeofencingApi.removeGeofences() methods.
To enable mock locations, you must first request the corresponding permission (usually for your debug build only):
Then you can enable and set mock location:
To distinguish if the location is a mock one, a key of FusedLocationProviderApi.KEY_MOCK_LOCATION in the location object’s bundle extra will be set to true.
Once done, please remember to set the mock mode to false. If you forget that, the system will set it to false when your location client is disconnected.
That’s it for today. Happy coding and keep reading.