1
我試圖在應用處於後臺時控制屏幕的亮度,並且我不確定這是否可行。iOS:在應用處於後臺時設置屏幕亮度
this UIScreen brightness documentation注意到
由一個應用程序作出亮度變化保持有效,直到該設備被鎖定,而不管該應用程序是否是關閉的。系統亮度(用戶可在設置或控制中心中設置)在下次打開顯示器時恢復。
這似乎並不是說您不能在後臺,應用程序主線程或其他位置設置屏幕亮度。它僅僅表示在鎖定/解鎖循環中亮度被重置。
考慮下面的代碼片段:
backgroundTask = UIApplication.shared().beginBackgroundTask(withName: "something", expirationHandler: handler) // handler ends the background task
registerCallbackForSomeEvent(callback: changeScreenBrightness)
事件無關。我可以在應用程序暫停時打印(),並且一切正常。
和changeSCreenBrightness()
實現是類似以下內容:
func changeScreenBrightness() {
let newBrightness = getNewBrightnessSomehow()
// main or global, it doesn't seem to be happening...
DispatchQueue.main.async(execute: {
// this works in the foreground?! but not the background..
UIScreen.main().brightness = newBrightness
// a print() will be executed however..
})
}
所以..任何人都可以肯定地說,這是不可能的?還是有人有我可以學習的工作代碼?
謝謝!