为了账号安全,请及时绑定邮箱和手机立即绑定

ContextCompat.startForegroundService

ContextCompat.startForegroundService

三国纷争 2021-08-04 10:14:21
正如问题标题所问的那样,我想知道它们的区别是什么,因为如果它们确实存在差异,文档就不是很清楚。
查看完整描述

1 回答

?
GCT1015

TA贡献1827条经验 获得超4个赞

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);

        }

    }


查看完整回答
反对 回复 2021-08-04
  • 1 回答
  • 0 关注
  • 285 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信