Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read real, current value of brightness/dim of the screen for Android?

Entire question is in the title, so I would like to emphasis as I can that:

  • I am interested in current value (not in current settings of it)

  • I am interested in real value (so if the brightness of the screen is now lower than a second before, the value should be lower as well)

Sadly, despite reading numerous posts, I don't know the answer. I wrote little utility, that every one second shows time and brightness. And it does not work -- because when the phone (LG L5 with Android 4.0) automatically lowers the brightness (the screen is dimmed, and there is no doubt about it) the values stay the same!

Here is the relevant piece of code:

try
{
  float sys_brightness = android.provider.Settings.System
                         .getFloat(getContentResolver(),
                                   android.provider.Settings
                                   .System.SCREEN_BRIGHTNESS);

  WindowManager.LayoutParams lp = getWindow().getAttributes();  
  float dim = lp.dimAmount;
  float scr_brightness = lp.screenBrightness;
  boolean dim_changed = (lp.flags & WindowManager.LayoutParams
                                    .DIM_AMOUNT_CHANGED)>0;  

  textView.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds)+" "
                   +String.format("%02f, %02f, %02f", sys_brightness,dim,scr_brightness)
                   +" "+Boolean.toString(dim_changed));
}
catch (SettingNotFoundException ex)
{
  textView.setText("excepton");
}

For the record, values are -- 92, 1.0, -1.0, false. All the time.

QUESTION -- how to read current, real brightness/dim value?

I added clock to output to be sure, my readings are ticking. And they are.

like image 223
greenoldman Avatar asked Nov 14 '22 01:11

greenoldman


1 Answers

If you do grep -r "SCREEN_BRIGHTNESS" * in android/system/frameworks (android source code) you will find: BrightnessController.java which is used to control brightness. Maybe you can use this class as inspiration.

like image 100
wojciii Avatar answered Dec 15 '22 09:12

wojciii