Change the System Brightness Programmatically

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

How to programmatically change screen brightness thru Intel graphics in C#

Try using

SetMonitorBrightness

Here's the Microsoft link for it.

Changing Screen brightness in 6.0 programmatically

Apparently you need to explicitly ask the user for permission on devices running Android 6.0+.

Try the following code: (I have modified it for you)

@TargetApi(Build.VERSION_CODES.M)
public void setBrightness(View view,int brightness){

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.System.canWrite(getApplicationContext()))
{
if (brightness < 46)
brightness = 255;
else if (brightness > 47)
brightness = 0;

ContentResolver cResolver = this.getApplicationContext().getContentResolver();
Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness);

} else

{
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + this.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}

Note that you need the WRITE_SETTINGS permission in your manifest, also.

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.

How to programmatically dim screen in UWP app

Update:

This is possible since Windows 10 Creators Update, see https://docs.microsoft.com/en-us/uwp/api/windows.graphics.display.brightnessoverride.


Unfortunately, there is no such API that can control the screen brightness in Universal Windows Platform (UWP). As @Rafael said, the screen brightness is thought to be a global setting controlled by the System Settings. And in some device families such as IoT, the device even may has no monitor.

If you want to dim the screen in your app, you can try to add a dark overlay on top of your view and control its opacity. Also, you are welcome to vote on UserVoice to ask for this feature.

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.

Can flutter change the brightness of the screen?

Yes Flutter can, you can use the screen plugin:
Screen plugin

This is an example of how to implement it, You just have to set _brightness for the start value you want and change it using the Slider:

import 'package:flutter/material.dart';
import 'package:screen/screen.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
bool _isKeptOn = false;
double _brightness = 1.0;

@override
initState() {
super.initState();
initPlatformState();
}

initPlatformState() async {
bool keptOn = await Screen.isKeptOn;
double brightness = await Screen.brightness;
setState((){
_isKeptOn = keptOn;
_brightness = brightness;
});
}

@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(title: new Text('Screen plugin example')),
body: new Center(
child: new Column(
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text("Screen is kept on ? "),
new Checkbox(value: _isKeptOn, onChanged: (bool b){
Screen.keepOn(b);
setState((){_isKeptOn = b; });
})
]
),
new Text("Brightness :"),
new Slider(value : _brightness, onChanged : (double b){
setState((){
_brightness = b;
});
Screen.setBrightness(b);
})
]
)
),
),
);
}
}

Let me know if you need any more explanation. This solution is only for Android and iOS, not desktop cases.



Related Topics



Leave a reply



Submit