As the question title asks, I would like to know what their differences are as documentation is not very clear if they actually have differences.
Thanks in advance.
ContextCompat
is utitility class for compatibility purposes.
context.startForegroundService
was introduced in Android Oreo(API 26) and is new correct way to start a foreground service. Before Android Oreo you had to just call startService
and thats what ContextCompat.startForegroundService
does. After API 26 it calls context.startForegroundService
or calls context.startService
otherwise.
Code from ContextCompat
API 27 sources.
/**
* 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#startForegeroundService()
* @see Context#startService()
*/
public static void startForegroundService(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= 26) {
context.startForegroundService(intent);
} else {
// Pre-O behavior.
context.startService(intent);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With