猿问

ContextCompat.startForegroundService

正如问题标题所问的那样,我想知道它们的区别是什么,因为如果它们确实存在差异,文档就不是很清楚。



三国纷争
浏览 286回答 1
1回答

GCT1015

ContextCompat 是出于兼容性目的的实用程序类。context.startForegroundService是在 Android Oreo(API 26) 中引入的,是启动前台服务的新正确方法。Android的奥利奥之前,你必须只调用startService和多数民众赞成什么ContextCompat.startForegroundService呢。在 API 26 之后,它会调用context.startForegroundService或context.startService以其他方式调用。来自ContextCompatAPI 27 来源的代码。/**     * 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);        }    }
随时随地看视频慕课网APP

相关分类

Java
我要回答