1

I was trying to switch between 'full brightness' and 'phones normal brightness' by using a switch button in my main activity. I successfully handled the switching of brightness by using this code:

switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
            if (bChecked) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                WindowManager.LayoutParams params = getWindow().getAttributes();
                params.screenBrightness = 1.0f;
                getWindow().setAttributes(params);
            } else {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                WindowManager.LayoutParams params = getWindow().getAttributes();
                params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
                getWindow().setAttributes(params);
            }
        }
    });

    if (switchButton.isChecked()) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.screenBrightness = 1.0f;
        getWindow().setAttributes(params);
    } else {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
        getWindow().setAttributes(params);
    }

The problem is,

after switching to 'full Brightness', when I change the activity, the brightness goes normal. Now, How can I keep track of the 'brightness setting' from 'main activity' and apply it to the other activities of the app?

N.B. I don't want to change system brightness. Brightness will only change while using the app.

Thanks In Advance.

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
Sayem
  • 78
  • 1
  • 10
  • 1
    One option: write the brightness settings to [SharedPreferences](https://developer.android.com/training/data-storage/shared-preferences) and read them in the Activity's onCreate() – Bö macht Blau Jun 15 '18 at 17:12
  • You can create a global variable for storing brightness setting. – Anand Jain Jun 15 '18 at 17:23
  • Thank you so much for the idea :) @0X0nosugar – Sayem Jun 16 '18 at 00:01
  • You're welcome :) - I just explained in my other comment that it's OK to write/ accept your own answer. The code snippet with the solution does belong in the answer not in the question, so I did a rollback on your edit – Bö macht Blau Jun 16 '18 at 17:50
  • its ok man. I appreciate it. I will mark answer. Thanks again for your idea :) @0X0nosugar – Sayem Jun 16 '18 at 17:57

2 Answers2

1

ok. Finally solved my problem.

  1. I have created a class having the logic of brightness controlling using SharedPreferences to Handle the state. code:

     if (br.getString("set", "").equals("1")) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.screenBrightness = 1.0f;
        getWindow().setAttributes(params);
    } else {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
        getWindow().setAttributes(params);
    }
    
  2. Then extend the class with the activities and initialize SharedPreferences in onCreate method.

     br = getSharedPreferences("br", Activity.MODE_PRIVATE);
    

Thank you @0X0nosugar for the idea.

Sayem
  • 78
  • 1
  • 10
  • Why don't you just update your answer to include the fixed code, something like a before and after? Although you haven't done this (yet), it seems odd when people accept their own answers (at least to me) – ViaTech Jun 15 '18 at 23:16
  • Hi. Thanks for your feedback. I have updated my post with solution. Its @0X0nosugar who gave me the idea. I just followed the things what he said. have a look at his comment. thank you. – Sayem Jun 15 '18 at 23:58
  • Yeah, shared preferences are perfect for storing settings on Android, it is a great suggestion. Thanks for the update to your post, that will also make it easier for people to read when they see this thread in the future. – ViaTech Jun 16 '18 at 12:33
  • @ViaTech - as far as I know, it's very much ok on Stack Overflow to share a solution to a problem by [answering your own question](https://stackoverflow.com/help/self-answer). It is also ok to accept the best answer, never mind if it's yours. Don't forget that Stack Overflow aims to help future readers, and one way of helping them is to show them what worked for others by voting on/ accepting helpful posts. – Bö macht Blau Jun 16 '18 at 17:45
  • @0X0nosugar, yeah what I was saying was more or less subjective about posting your own answers, I've done it too when needed, I was just saying it seems easier, as a reader, if the OP answers thier own question inside the original post – ViaTech Jun 16 '18 at 21:34
0

You need to hold a SCREEN_BRIGHT_WAKE_LOCK if you want it to have an effect beyond your activity. The method you are using above applies to the foreground window, and when you activity goes away that no longer applies.

Note Android generally frowns on use of wake locks because it's easy to screw up and not release them, wasting the devices battery. They recommend the window param version for the vary reason that it automatically releases when the user leaves the activity.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • hi, thanks for your valuable information. Can you please refer any example so that I can get a clear idea about WAKE_LOCK? I am not familiar with it. @JeffreyBlattman – Sayem Jun 15 '18 at 20:22