2017-08-04 48 views
5

migration guide中,建議在必要時使用NotificationManager.startServiceInForeground()而不是startService()Android O後臺服務限制

但我看不到任何startServiceInForeground()方法在NotificationManager。我錯過了什麼?

+0

這不是在NotificationManager,但在活動/上下文(例如其中startService現在) – RobCo

+0

你必須compileSdkVersion設置爲26和升級支持庫26.0.0看到Android的推出O. –

+1

出於兼容性的方法在26.0.0-beta1支持庫中使用'ContextCompat.startForegroundService()' – RobCo

回答

3

您需要更改文檔站點的API級別看到新方法

screenshot

您應該使用@RobCo提出以下方法在26.0.0-β1支持庫版本中增加了

ContextCompat.startForegroundService() 

是的,這樣做的遷移指南是錯誤的,NotificationManager沒有一個叫startForegroundService()方法,即使在API 26

PS:遷移指南已更正,現在它正確鏈接到ContextCompat.startForegroundService()方法。

+0

是的,的確我已經在顯示API 26的文檔了,但事實證明這個方法根本不在'NotificationManager'中,而是在Context中......將使用'ContextCompat'來代替。 – drmrbrewer

0

你不會錯過任何東西。看起來谷歌忘了更新文檔。

0

它們尚未在遷移指南中更改。

您應該使用ContextCompat.startForegroundService()而不是NotificationManager.startServiceInForeground()方法。