Effortless Location Tracking with FusedLocationProviderClient and PendingIntents in Android

Akkic
3 min readApr 16, 2024

--

[If you are Looking for Kotlin code and callback implementation or implementing fusedLocationProviderClient with callback method please check our story FusedLocationTracking with Kotlin + callback

If you are not able to receive Location callbacks when your app is in killed state. You are in right place to solve your problemšŸ˜

Introduction:

Location tracking is a crucial feature for many Android applications, enabling a wide range of functionalities, from navigation to context-aware services. The FusedLocationProviderClient, part of the Google Play services location APIs, offers a powerful and efficient way to retrieve device location information. In this article, weā€™ll explore how to integrate the FusedLocationProviderClient into an Android app using PendingIntents, a mechanism that allows us to receive location updates asynchronously.

Understanding PendingIntents:

PendingIntents are a way to perform an action on your appā€™s behalf, even when the app itself is not running. They are essentially tokens that the Android system can use to send an intent to your app at a later time, allowing your app to respond to system events or scheduled operations.

In the context of location tracking, PendingIntents provide a convenient way to receive location updates without keeping your appā€™s components (e.g., activities or services) active all the time. This approach can significantly improve battery life and overall performance.

Integrating the FusedLocationProviderClient with PendingIntents:

Hereā€™s how you can integrate the FusedLocationProviderClient into your Android app using PendingIntents:

1. Obtain the FusedLocationProviderClient instance:

fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

2. Request necessary permissions: Your app will need to request the `ACCESS_FINE_LOCATION` permission to access the deviceā€™s location. If youā€™re targeting Android 10 (API level 29) or higher and your app needs to access the location while running in the background, youā€™ll also need to request the `ACCESS_BACKGROUND_LOCATION` permission.

3. Configure location request parameters: Create a `LocationRequest` object and set the desired parameters, such as update intervals, priority, and accuracy.

LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

4. Create a PendingIntent: Construct a PendingIntent to receive location updates asynchronously.

Intent intent = new Intent(this, LocationUpdatesBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

NOTE ā€” Make sure your pending intent is not marked as IMMUTABLE, which will return as null in your receiver class

5. Register for location updates: Use the `requestLocationUpdates` method of the FusedLocationProviderClient to start receiving location updates, passing the `LocationRequest` and the `PendingIntent`.

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
fusedLocationClient.requestLocationUpdates(locationRequest, pendingIntent);
}

6. Handle location updates: Create a `BroadcastReceiver` to handle the incoming location updates from the PendingIntent.

public class LocationUpdatesBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
LocationResult locationResult = LocationResult.extractResult(intent);
if (locationResult != null) {
Location location = locationResult.getLastLocation();
// Handle the received location update
Log.d("LocationUpdate", "Latitude: " + location.getLatitude() + ", Longitude: " + location.getLongitude());
}
}
}
}

7. Stop location updates when not needed: Donā€™t forget to stop location updates when your app no longer requires them, for example, in the `onStop` or `onPause` methods of your activity or fragment.

fusedLocationClient.removeLocationUpdates(pendingIntent);
pendingIntent.cancel()

By following these steps, youā€™ll be able to seamlessly integrate the FusedLocationProviderClient into your Android application using PendingIntents. This approach allows your app to receive location updates asynchronously, without the need to keep components active, resulting in better battery life and overall performance.

Conclusion:

The FusedLocationProviderClient, combined with PendingIntents, provides a powerful and efficient way to implement location tracking in Android applications. By leveraging PendingIntents, your app can receive location updates asynchronously, improving battery life and overall performance. With the provided code examples and explanations, you should now have a solid understanding of how to integrate the FusedLocationProviderClient using PendingIntents into your Android app.

--

--

Akkic

Mobile App Developer (React-Native, Android, iOS, flutter and Cordova Applications). My Stories based on personal experienced which were time taken to solvešŸ˜