Adjust screen brightness in Mac OS X app

There’s no such nice API for doing this on OS X. We have to use IOServiceGetMatchingServices to find “IODisplayConnect” (the display device) then use the kIODisplayBrightnessKey to set the brightness: func setBrightnessLevel(level: Float) { var iterator: io_iterator_t = 0 if IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching(“IODisplayConnect”), &iterator) == kIOReturnSuccess { var service: io_object_t = 1 while service != 0 … Read more

Android: detect brightness (amount of light) in phone’s surroundings using the camera?

Here is how you register a listener on the light sensor: private final SensorManager mSensorManager; private final Sensor mLightSensor; private float mLightQuantity; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Obtain references to the SensorManager and the Light Sensor mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); // Implement a listener to receive updates SensorEventListener listener = … Read more

Can’t apply system screen brightness programmatically in Android

OK, found the answer here: Refreshing the display from a widget? Basically, have to make a transparent activity that processes the brightness change. What’s not mentioned in the post is that you have to do: Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE, 0); Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, brightnessLevel); then do WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = brightness; getWindow().setAttributes(lp); And if you call finish() right … Read more

Changing screen brightness programmatically (as with the power widget)

This is the complete code I found to be working: Settings.System.putInt(this.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 20); WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness =0.2f;// 100 / 100.0f; getWindow().setAttributes(lp); startActivity(new Intent(this,RefreshScreen.class)); The code from my question does not work because the screen doesn’t get refreshed. So one hack on refreshing the screen is starting dummy activity and than in on create … Read more

Change the System Brightness Programmatically

You can use following: // Variable to store brightness value private int brightness; // Content resolver used as a handle to the system’s settings private ContentResolver cResolver; // Window object, that will store a reference to the current window private Window window; In your onCreate write: // Get the content resolver cResolver = getContentResolver(); // … Read more