2016-11-29 54 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后
  • 命令不运行
相关问题