Skip to content Skip to sidebar Skip to footer

Location While In Background Android Oreo

I want to build an app like Run Keeper for tracking while out on an actitivty. What is the best way to track the users location while the phone is locked for example. The user will

Solution 1:

For Oreo and above you must call ContextCompat.startForegroundService(this, serviceIntent); while launching the service instead of startService(serviceIntent)

source code inside ContextCompat looks like this

/**
 * startForegroundService() was introduced in O, just call startService
 * for before O.
 *
 * @param context Context to start Service from.
 * @param intent The description of the Service to start.
 *
 * @see Context#startForegroundService(Intent)
 * @see Context#startService(Intent)
 */publicstaticvoidstartForegroundService(@NonNull Context context, @NonNull Intent intent) {
    if (Build.VERSION.SDK_INT >= 26) {
        context.startForegroundService(intent);
    } else {
        // Pre-O behavior.context.startService(intent);
    }
}

And inside the service you must call startForeground(int id, Notification notification) as soon as service starts

Solution 2:

You should read oreo update related files.

By default, these changes only affect apps that target Android 8.0 (API level 26) or higher. However, users can enable these restrictions for any app from the Settings screen, even if the app targets an API level lower than 26. You may need to update your app to comply with the new limitations.

Check to see how your app uses services. If your app relies on services that run in the background while your app is idle, you will need to replace them. Possible solutions include:

If your app needs to create a foreground service while the app is in the background, use the startForegroundService() method instead of startService().

If you want to know more about https://developer.android.com/about/versions/oreo/background

Post a Comment for "Location While In Background Android Oreo"