Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to restore brightness on application exit

I have a feature in my app (a metronome app for musicians) to dim the screen in order to save on battery life. To set the brightness, I am using:

[UIScreen mainScreen].brightness = 0.1;

I am saving the original brightness on app start up in viewDidLoad(...) and saving that to my User Defaults.

When changing views within the app, I retrieve the original brightness from User Defaults and restore with a call:

[UIScreen mainScreen].brightness = originalBrightness;

This works fine. I have NSLog(...) messages showing the original value, etc... so the mechanism works.

The issue I am having is how to restore the original brightness on application exit as the Home button is pressed.

I have added similar code to my application delegate methods for:

applicationWillResignActive(...)
applicationDidEnterBackground(...)
applicationWillTerminate(...)

They each have a method call to set the screen brightness as before in the view with similar NSLog(...)s showing the retrieved original brightness...and it is all correct. I have also debugged in and the calls to set brightness are being made...but the app exits, and the device screen is still at the lower, dimmed level.

Finally, if you click the "lock" button on top, then press the Home button to wake it up...the brightness is correct.

My suspicion is that whatever action or event is triggered with my call to set the brightness when the app is exiting is not getting through, maybe due to an invalid state or similar.

Also, my app is set to NOT run in the background, set in the info.plist as:

Application does not run in background     YES

Any help would be appreciated.

Thanks!

like image 638
TheTwoNotes Avatar asked Feb 29 '12 23:02

TheTwoNotes


1 Answers

iOS allows that app a little bit of time before exiting. You might try a sleep for a fraction of a second after setting the brightness.

like image 115
picciano Avatar answered Nov 06 '22 19:11

picciano