Can't Apply System Screen Brightness Programmatically in Android

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 after applying the changes, brightness will never actually change because the layout has to be created before the brightness settings is applied. So I ended up creating a thread that had a 300ms delay, then called finish().

changing screen brightness programmatically in android

How about using the IHardwareService interface for this? An example can be found in this tutorial.

Update: tutorial link still works, but actual code is also available in next answer.

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();

// Get the current window
window = getWindow();

try {
// To handle the auto
Settings.System.putInt(
cResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
);
// Get the current system brightness
brightness = Settings.System.getInt(
cResolver, Settings.System.SCREEN_BRIGHTNESS
);
} catch (SettingNotFoundException e) {
// Throw an error case it couldn't be retrieved
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}

Write the code to monitor the change in brightness.

then you can set the updated brightness as follows:

// Set the system brightness using the brightness variable value
Settings.System.putInt(
cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness
);
// Get the current window attributes
LayoutParams layoutpars = window.getAttributes();
// Set the brightness of this window
layoutpars.screenBrightness = brightness / 255f;
// Apply attribute changes to this window
window.setAttributes(layoutpars);

Permission in manifest:

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

For API >= 23, you need to request the permission through Settings Activity, described here:
Can't get WRITE_SETTINGS permission

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 of that dummy activity to call finish() so the changes of the brightness take effect.

android Programmatically set screen brightness and keep it

This is happening because your activity is restarting.
You can try adding your window settings code in onCreate of your activity.
Make sure that this code is added before setting the view of the activity.

 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

final WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 0.5f;
getWindow().setAttributes(lp);

setContentView(R.layout.your_layout_id);
}

Alter system brightness within a service

Got a solution! GeekYouUp's method here works: Update Display Brightness on Android after changing it programmatically

You need to make sure you finish() the activity with a small delay, that was my main issue. Sure it's not 'clean' to have a transparent activity pop up, but what can you do? I'm quite sure that there's no other way.

Changing screen brightness efficiently

The following affects immediately the single activity, no need to restart it. The activity also remembers the screenBrightness attribute over pause/resume.

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 1; // 0f - no backlight ... 1f - full backlight
getWindow().setAttributes(lp);

But it has no effect if you have automatic backlight level enabled in the system settings. This solution should help to turn off automatic backlight.



Related Topics



Leave a reply



Submit