1 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊
ContextCompat 是出于兼容性目的的實(shí)用程序類。
context.startForegroundService是在 Android Oreo(API 26) 中引入的,是啟動(dòng)前臺(tái)服務(wù)的新正確方法。Android的奧利奧之前,你必須只調(diào)用startService和多數(shù)民眾贊成什么ContextCompat.startForegroundService呢。在 API 26 之后,它會(huì)調(diào)用context.startForegroundService或context.startService以其他方式調(diào)用。
來(lái)自ContextCompatAPI 27 來(lái)源的代碼。
/**
* 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);
}
}
添加回答
舉報(bào)