Changing Screen Brightness Programmatically in Android

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 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.

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.

Getting the current screen brightness

To my knowledge, it cannot be done any other way in Auto mode. See this answer.

Change brightness according to surrounding light in android

Solution from myself:

I implemented shaking listener from here.
I used aboce links also to make it completed.

I created ShakeBrightService.java

public class ShakeBrightService extends Service {

private ShakeDetector shakeBrightDetector = new ShakeDetector();

@Override
public void onCreate() {
super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

shakeBrightDetector.stop();
shakeBrightDetector.start(this, shakeBrightEventListener);

return super.onStartCommand(intent, flags, startId);
}

private ShakeEventListener shakeBrightEventListener = new ShakeEventListener() {

@Override
public void onShake() {
setAutoBrightness(true);
}
};

@Override
public IBinder onBind(Intent intent) {
return null;
}

private void setAutoBrightness(boolean value) {

Toast.makeText(getApplicationContext(), "AutoBrightness : " + value,
Toast.LENGTH_SHORT).show();

if (value) {

Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
} else {

Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}

Intent i = new Intent(getApplicationContext(), MainActivity1.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}

}

Fake for refreshing view
MainActivity1.java

public class MainActivity1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main1);

refreshBrightness(getBrightnessLevel());

Thread t = new Thread() {
public void run() {
try {
sleep(10);
} catch (InterruptedException e) {
}
finish();
}
};
t.start();
}

private void refreshBrightness(float brightness) {

WindowManager.LayoutParams lp = getWindow().getAttributes();
if (brightness < 0) {
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
} else {
lp.screenBrightness = brightness;
}
getWindow().setAttributes(lp);
}

int getBrightnessLevel() {
try {
int value = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS);
// convert brightness level to range 0..1
value = value / 255;
return value;
} catch (SettingNotFoundException e) {
return 0;
}
}

FakeActivity is having theme android:theme="@android:style/Theme.Translucent.NoTitleBar", so silently brightness is changed.



Related Topics



Leave a reply



Submit