2016-11-29 49 views
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.. 
    }) 
} 

所以..任何人都可以肯定地說,這是不可能的?還是有人有我可以學習的工作代碼?

謝謝!

回答

0

我一直在嘗試做類似的事情。我試探性地斷定,當應用程序不在前臺時,此功能被禁用(儘管我仍在搜索文檔以支持此功能)。

但是,一旦設備被鎖定,用戶的原始亮度IS就會恢復。

我想在最小化或退出時將用戶恢復到其原始UI亮度。我目前使用下面的代碼:

func _onApplicationLoad(){ 
    NotificationCenter.default.addObserver(self, selector: #selector(_onApplicationMinimise), name: Notification.Name.UIApplicationWillResignActive, object: nil) 
} 

func _onApplicationMinimise(){ // This function IS called on minimise 
    // commands here run ok 
    UIScreen.main.brightness = CGFloat(1.0) // this doesn't run 
    // commands here run ok too 
} 
  • _onApplicationMinimise()函數被調用和作品。
  • 亮度線運行前的命令。亮度線運行
  • UIScreen.main.brightness = 1.0後
  • 命令不運行
相關問題