1

I am trying to display a toast to the user and dim the screen to its minimum brightness after 5 seconds. But the screen didn't dim at all. Did I make any mistake in my codes? Please help.

Codes:

private void DimScreen()
    {
            Toast.makeText(StartActivity.this, "Dimming screen in 5 seconds, press Stop button to turn on the screen", Toast.LENGTH_SHORT).show();
            handler.postDelayed(r, 5000);
    }

private Handler handler= new Handler();
    private Runnable r = new Runnable()
    {
        public void run()
        {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness=0.01f;
            getWindow().setAttributes(lp);  

        }
    };
qwr qwr
  • 1,049
  • 4
  • 22
  • 46
  • 2
    Is your device [on auto brightness](http://stackoverflow.com/questions/4611287/changing-screen-brightness-on-a-htc-sense-device)? Also, add a toast after the brightness change--does it display? – Cat Aug 31 '12 at 04:57
  • No. I add log after the setAttributes(lp), and it got displayed properly on logcat. – qwr qwr Aug 31 '12 at 05:02
  • I tried toast as well, it got displayed as well. – qwr qwr Aug 31 '12 at 05:31
  • Why 0.01f? Why not 0? http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#BRIGHTNESS_OVERRIDE_OFF – auselen Aug 31 '12 at 05:40
  • 0 will make the screen completely dark and the lock screen will appear when the user try to turn back on the device – qwr qwr Aug 31 '12 at 05:45
  • 0.01 is very close to minimum I think – qwr qwr Aug 31 '12 at 05:46

1 Answers1

0

Do you get the right permissions in your Manifest?

You should have this:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

If that still does not work, you might want to use the code like this:

android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, SysBackLightValue);

See: http://android-er.blogspot.tw/2011/02/change-system-screen-brightness-using.html

Also, I am not sure you would be able to do that outside of the UI thread... If that ends up being the problem, you can use runOnUiThread

Matthieu
  • 16,103
  • 10
  • 59
  • 86
  • My code is pretty much the same with the link u posted, I am wondering why you need to set the android.provider though. – qwr qwr Aug 31 '12 at 05:07
  • Did you try to run from UI thread instead of your other thread? – Matthieu Aug 31 '12 at 06:29
  • I think it is running on the UI thread because i can do a toast after getwindow().setattribute(). – qwr qwr Aug 31 '12 at 06:41
  • What if you copy the code to dim the screen just after the toast you already have there and instead of posting to the handler (it should dim the screen right away). Does that work? – Matthieu Aug 31 '12 at 06:49
  • I tried to call these 3 lines every where on my activity and it seems like my activity has no response to them at all....I tried to call getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); as well And my activity didn't response as well... – qwr qwr Aug 31 '12 at 15:11
  • could it be the phone you are using is preventing that? Can you try different phones? Emulator? – Matthieu Sep 03 '12 at 00:34