1 回答
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);
}
}
添加回答
举报