Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ContextCompat.startForegroundService(context, intent) and startforegroundservice(intent)?

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.

like image 650
YSY Avatar asked Aug 20 '18 20:08

YSY


1 Answers

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);
        }
    }
like image 116
Tuby Avatar answered Nov 15 '22 16:11

Tuby