正如问题标题所问,我想知道它们的差异是什么,因为文档不是很清楚它们是否确实存在差异。
提前致谢。
ContextCompat
是出于兼容性目的的实用程序类。
context.startForegroundService
是在 Android Oreo(API 26)中引入的,是启动前台服务的新的正确方法。在 Android Oreo 之前,您只需调用startService
就是这样ContextCompat.startForegroundService
做。在 API 26 之后它调用context.startForegroundService
或致电context.startService
否则。
代码来自ContextCompat
API 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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)