Reputation: 2293
I am developing a flashlight application and I want to have it set up to where when the user opens the application, the screen brightness goes to full. I have set up a checkbox preference to do this but have a slight problem. See code below:
<CheckBoxPreference
android:defaultValue="true"
android:key="pref_brightness"
android:summary="When checked, brightness will raise to highest level on main activity."
android:title="@string/pref_5" />
This is the checkbox code in the xml file for the preferences layout.
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean box = getPrefs.getBoolean("pref_brightness", true);
if (box == true) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 100.0f;
getWindow().setAttributes(lp);
}
And here is where I access the preference inside the MainActivity.java file. This is in the onCreate() method.
Now my problem is...when I open the application, the box is checked (unless altered by the user) and the brightness maximizes like it should. However, when I go into the preferences and uncheck the box, then press the "back" button, the box saves but nothing takes effect on the main activity. If I change the box and press the "up" button on the action bar, everything works great. Why is it not working when the back button is pressed? I tried using onBackPressed() but nothing seemed to fix this issue.
I did what you said and it still doesn't work right. If the box is unchecked and I go check it, press the back button, it works but then if I go change it back again, press back again, it doesn't save. Here's the code for your suggestion:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean box = getPrefs.getBoolean("pref_brightness", true);
if (box == true) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 100.0f;
getWindow().setAttributes(lp);
}
super.onWindowFocusChanged(hasFocus);
}
Help is appreciated,
Andrew
Upvotes: 0
Views: 76
Reputation: 2293
Solved!
I just needed a full if statement like so:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean box = getPrefs.getBoolean("pref_brightness", true);
if (box == true) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 100.0f;
getWindow().setAttributes(lp);
}
else{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = -1;
getWindow().setAttributes(lp);
}
super.onWindowFocusChanged(hasFocus);
}
Now this says if check box is checked, change the main activity's brightness to full but if it's unchecked, set the user's brightness to their setting.
Thanks to Karakuri for the help as well as Nizam!
Andrew
Upvotes: 0
Reputation: 38605
If you run this code in onCreate()
, it will only get called once -- when the Activity is being created. You need to run it somewhere else. I recommend onWindowFocusChanged()
. (You could use onStart()
or onResume()
, but those won't take into account the lock screen.)
Upvotes: 1
Reputation: 5731
The code that changes the attributes shouldn't be inside onCreate()
.Try using inside onStart()
.
Upvotes: 1