Location Updates
Contents
Android location updates can be requested using the FusedLocationProviderClient
class. You will need to add the play services location library to your app.
implementation "com.google.android.gms:play-services-location:16.0.0"
Location Permissions
In order to receive location updates you will need to have the correct permissions. These should be added to theAndroidManifest.xml
for your app.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
These permissions are not granted automatically and will also need to be requested while your app is running. You can check and request permissions like so:
Java
if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
int requestCode = 123; // Define your request code
// request permissions
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, requestCode);
}
Kotlin
if (ContextCompat.checkSelfPermission(applicationContext, Manifest.permission.ACCESS_FINE_LOCATION) !== PackageManager.PERMISSION_GRANTED) {
val requestCode = 123 // Define your request code
// request permissions
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION), requestCode)
}
Requesting Location Updates
Once you have received permission, the calling activity will be informed of the result in theonRequestPermissionsResult
method. You can now register for location updates. One option for receiving these is in a BroadcastReceiver
. We will be using this method for our guide.
First you should create the BroadcastReceiver
:
Java
public class LocationBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(LocationResult.hasResult(intent)) {
LocationResult locationResult = LocationResult.extractResult(intent);
Location location = locationResult.getLastLocation();
new SailthruMobile().updateLocation(location);
}
}
}
Kotlin
class LocationBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (LocationResult.hasResult(intent)) {
val locationResult: LocationResult = LocationResult.extractResult(intent)
val location: Location = locationResult.lastLocation
SailthruMobile().updateLocation(location)
}
}
}
updateLocation
method when it is received. You should then add the LocationBroadcastReceiver
to the manifest.
<receiver android:name=".LocationBroadcastReceiver"
android:exported="false"/>
You can then request location updates to this receiver through the FusedLocationProviderClient
(once you have permission).
Java
// setup intent for BroadcastReceiver
Intent locationServiceIntent = new Intent(this, LocationBroadcastReceiver.class);
// create pending intent for location services
PendingIntent locationIntent = PendingIntent.getBroadcast(this, requestCode, locationServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// get FusedLocationProviderClient
FusedLocationProviderClient locationProviderClient = LocationServices.getFusedLocationProviderClient(this);
// setup location request
long locationUpdateInterval = 180000; // update interval in milliseconds
long locationMaxUpdateInterval = 300000; // max update interval in milliseconds
LocationRequest locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setInterval(locationUpdateInterval).setMaxWaitTime(locationMaxUpdateInterval);
// request updates
locationProviderClient.requestLocationUpdates(locationRequest, locationIntent);
Kotlin
// setup intent for BroadcastReceiver
val locationServiceIntent = Intent(this, LocationBroadcastReceiver::class.java)
// create pending intent for location services
val locationIntent = PendingIntent.getBroadcast(this, requestCode, locationServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT)
// get FusedLocationProviderClient
val locationProviderClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
// setup location request
val locationUpdateInterval: Long = 180000 // update interval in milliseconds
val locationMaxUpdateInterval: Long = 300000 // max update interval in milliseconds
val locationRequest: LocationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setInterval(locationUpdateInterval).setMaxWaitTime(locationMaxUpdateInterval)
// request updates
locationProviderClient.requestLocationUpdates(locationRequest, locationIntent)
Java
// setup intent for BroadcastReceiver
Intent locationServiceIntent = new Intent(this, LocationBroadcastReceiver.class);
// create pending intent for location services
PendingIntent locationIntent = PendingIntent.getBroadcast(this, requestCode, locationServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// remove location updates
locationProviderClient.removeLocationUpdates(locationIntent);
Kotlin
// setup intent for BroadcastReceiver
val locationServiceIntent = Intent(this, LocationBroadcastReceiver::class.java)
// create pending intent for location services
val locationIntent = PendingIntent.getBroadcast(this, requestCode, locationServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT)
// remove location updates
locationProviderClient.removeLocationUpdates(locationIntent)