1

I try to toggle the screen brightness low and high periodically (1s) and I thought this code should work:

SystemClock.sleep(1000);
params.screenBrightness = 0;
getWindow().setAttributes(params);


SystemClock.sleep(1000);
params.screenBrightness = 1;
getWindow().setAttributes(params);

I have tried these codes but it only completes the second one (or last one if i extend the codes) (i.e. brightness=1). As I doubt about that so I put a variable int i = 0, then i++ after each sleep function, it shows me i = 2 after all (by displaying string). I think Android does the sum but my screen just react to the last setting but not the intermediate commands. Do you have any idea why it is that and how can I toggle the screen brightness?

I also try to use "for" loop but no luck.

Hope to receive your comments asap.

Cheers,

notebook
  • 29
  • 2
  • 6
  • 1
    So `params.screenBrightness = 1;` works but only if it's the last one, and `params.screenBrightness = 0;` also works, but only if it's the last one? – Steve Blackwell Aug 31 '12 at 17:50

2 Answers2

5

I'm not sure why you want to brighten and darken your screen every other second... But if you want run code on a time delay consider using a Handler and Runnable:

import android.view.WindowManager.LayoutParams;
public class Example extends Activity {
    private LayoutParams mAttributes;
    private Handler mHandler = new Handler();
    private Window mWindow;

    private Runnable onEverySecond = new Runnable() {
        public void run() {
            if(mAttributes.screenBrightness != LayoutParams.BRIGHTNESS_OVERRIDE_FULL)
                mAttributes.screenBrightness = LayoutParams.BRIGHTNESS_OVERRIDE_FULL;
            else
                mAttributes.screenBrightness = LayoutParams.BRIGHTNESS_OVERRIDE_OFF;

            mWindow.setAttributes(mAttributes);
            mHandler.postDelayed(onEverySecond, 1000);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWindow = getWindow();
        mAttributes = mWindow.getAttributes();

        mHandler.post(onEverySecond);
    }
}
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Wouldn't he also need a `mHandler.post(onEverySecond, 1000);` at the end of `run()`? – Steve Blackwell Aug 31 '12 at 17:57
  • @SteveBlackwell Yes, I cut a little too much from my example... Thanks! I added postDelayed(). – Sam Aug 31 '12 at 17:59
  • Thanks for your prompt help. I tried the code but no luck. Though the mHandler runs and alternates the screen brightness but if I have more it does not work. THis is what I add: It works for one time only and the last – notebook Aug 31 '12 at 21:46
  • Thanks! it works but not repeating. I created onEverySecond0 and onEverySecond1 for toggle Low and High brightness levels, respectively, then call mHandler.post(onEverySecond1); and mHandler.post(onEverySecond0);... subsequently but it still gets the last one – notebook Aug 31 '12 at 22:03
  • @notebook I updated my example code, notice how it only uses one Runnable to alternate brightness levels. I hope this helps. – Sam Aug 31 '12 at 23:26
  • Sam, it works fine. Thanks a lot. I have learnt some from you. I notice that the screen on/off time (transient time) is different from devices. For samsung S3 and Note it takes time to fade and bright up but for HTC OneX it is promptly switched. Is there any better code to force all devices go on/off quickly as i want to make the delay time is less than 1s, could be 10-100ms. By the way, for my curiosity, why my code with sleep() and set brightness does not work as logically Android runs the commands subsequently? as I just want to toggle screen for a certain number of cycles. Many thanks! – notebook Sep 01 '12 at 22:16
0

You can solve that either with a Handler and posting delayed tasks in runnables to it, or by using a timer. I'd go for the second approach since what you need is to repeat tasks rather than executing tasks sequentially.

Community
  • 1
  • 1
Jose L Ugia
  • 5,960
  • 3
  • 23
  • 26
  • I have tried both cases but it does not work. If you could give more detail based on my above codes that is appreciated. Cheers – notebook Aug 31 '12 at 21:52