Document Location SDK for Android Guides Utility Tools Android 8.0 Permissions

Android 8.0 Permissions

Starting with Android 8.0 (API level 26), the system limits how frequently background apps can retrieve the user's current location to reduce power consumption. Background apps can only receive location updates a few times per hour. For more details, see the official Android documentation.

To increase the frequency of location updates, Google recommends that background apps display a foreground service notification. If you prefer to implement this yourself following the official guidelines, refer to our GitHub example.

Starting with Location SDK v3.8.0, this process is simplified. The SDK provides a single API call that creates a foreground service notification for your app. When your app moves to the background, the notification remains active, allowing your app to bypass the Android 8.0 background location restrictions. This document describes how to use this feature.

Step 1: Create a Notification

You must create a notification channel and notification. The following code provides a basic example. Customize it to fit your app's requirements.

private static final String NOTIFICATION_CHANNEL_NAME = "BackgroundLocation";
private NotificationManager notificationManager = null;
boolean isCreateChannel = false;

@SuppressLint("NewApi")
private Notification buildNotification() {

    Notification.Builder builder = null;
    Notification notification = null;
    
    if (android.os.Build.VERSION.SDK_INT >= 26) {
        // Android O introduced changes to Notification. If your targetSdkVersion >= 26,
        // use this approach to create the notification channel.
        if (null == notificationManager) {
            notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        String channelId = getPackageName();
        if (!isCreateChannel) {
            NotificationChannel notificationChannel = new NotificationChannel(channelId,
                    NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            notificationChannel.enableLights(true); // Show a dot on the app icon
            notificationChannel.setLightColor(Color.BLUE); // Dot color
            notificationChannel.setShowBadge(true); // Show this channel's notifications when long-pressing the app icon
            notificationManager.createNotificationChannel(notificationChannel);
            isCreateChannel = true;
        }
        builder = new Notification.Builder(getApplicationContext(), channelId);
    } else {
        builder = new Notification.Builder(getApplicationContext());
    }
    
    builder.setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(Utils.getAppName(this))
            .setContentText("Running in the background")
            .setWhen(System.currentTimeMillis());

    if (android.os.Build.VERSION.SDK_INT >= 16) {
        notification = builder.build();
    } else {
        return builder.getNotification();
    }
    return notification;
}

Step 2: Display the Notification When Using Background Location

Call the background location API when your app moves to the background or when you need to display the foreground notification.

// Enable background location. The first parameter is the notification ID.
// It is recommended to use a single ID throughout your app.
locationClient.enableBackgroundLocation(2001, buildNotification());

Step 3: Disable Background Location and Remove the Notification

When the notification is no longer needed, call the disable background location API.

// Disable background location. Pass true to remove the notification;
// pass false to keep it (you can still remove it manually).
locationClient.disableBackgroundLocation(true);

Important Notes

1. If your app already has a foreground service notification active when it moves to the background, you do not need to call enableBackgroundLocation() again.

2. It is recommended that only one AMapLocationClient instance in your app calls enableBackgroundLocation() and disableBackgroundLocation(). If multiple AMapLocationClient instances call enableBackgroundLocation(), the notification will only be removed when all of those instances have called disableBackgroundLocation(true).

3. The enableBackgroundLocation() and disableBackgroundLocation() APIs only manage the foreground service notification. They do not start or stop location updates. To control location updates, use AMapLocationClient.startLocation() and AMapLocationClient.stopLocation().