Changing screen brightness programmatically (as with the power widget)
Asked Answered
P

5

30

I was searching how to change the brightness of the screen programmatically and I found this it is very good solution and it works nice, but it works only while my app is active. After my application is shutdown then the brightness is returned back the the same value as before I start my app.

I want to be able to change the brightness just like when I press on the brightness button from my power widget. In the power widget that comes from android there are 3 states. One very bright one very dark and one in between. Is it possible to change the brightness just like when someone press on this widget ?

enter image description here

Edit1: I created this and I added permision to my manifest but when the app is started I do not see any changes to the brightness, I tried with 10 with 100 and now with 200 but no changes any suggestions ?

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.provider.Settings.System.putInt(this.getContentResolver(),
        android.provider.Settings.System.SCREEN_BRIGHTNESS, 200);
}
Peewee answered 4/10, 2011 at 11:1 Comment(1)
have you tryed these #3866383 not shore if the do change custom brightness propretyClaytonclaytonia
P
40

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.

Peewee answered 5/10, 2011 at 8:4 Comment(7)
Odd that you have to do that. :/ What phone are you using?Courtund
It is not odd, I use htc desire, I tried the code on samsung galaxy also... It is not device issue it is how the android works, it simply doesn't apply the brightness until the win manager get refreshed and he get refreshed by pressing the back, home button or by creating new activity ...Peewee
maybe if you use a widget doesn't need to be refreshed but in case you put the code in oncreate , it definitely need additional activity to be started,(at least this was my case) without that dummy activity the app doesn't work propertyPeewee
@Peewee I used intent to call dummy activity in my AppWidget class, but it force close.Oligosaccharide
This worked for 2.3 on HTC desire (but I never tried this on 4.0)Peewee
Will this stick for other activites, or will it only take effect for the current activity? Suppose I want to allow my users to autodim their phone if they choose to. I don't want this code to hinder that.Sinnard
how can i change brightness using button click with values like 100% , 50% , 10 % , 20%, auto and then again 100% , 50% etc @PeeweeIntercollegiate
C
13

Use Tor-Morten's solution in that link, but also set the system brightness setting like so:

android.provider.Settings.System.putInt(getContext().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, bright);

Where bright is an integer ranging from 1 to 255.

Courtund answered 4/10, 2011 at 11:8 Comment(8)
I get some exception I guess that I do not have some permission,do you know the permissionPeewee
Does someone know what permissions do I need ?Peewee
I add the permission <uses-permission android:name="android.permission.WRITE_SETTINGS" /> and it doesn't throw exception but this doesn't change the brightness at all, can you tall me what is the problem ?Peewee
Try executing the above code before adjusting the Window brightness. I think you may have to wait a small amount of time before you call finish() on the Activity too. So if you're closing it immediately after setting the brightness, perhaps add a small delay.Courtund
After I run the activity it stays active I do not close it , but still it doesn't no change the brightness. Is there any place where I can see a complete running code ?Peewee
@Peewee its working for values 10,20 but not for 50,100 why ?Intercollegiate
I think it did adjust but it doesn't refresh the window, so the current brightness don't work. I don't know how to solve it....Affer
For this answer to work, what I did was to set brightness mode manual first followed by setting the brightness level. Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);Intellectual
T
10

I have solved this problem today.

As the first you have to put a permission into your AndroidManifest.xml file:

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

Where is the exact place to put it in the file?

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

This permission says, that you are allowed to change settings that affect other applications too.

Now you can set brightness automatic mode on and off

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);  //this will set the automatic mode on
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);  //this will set the manual mode (set the automatic mode off)

Is the automatic mode turned on or off right now? You can get the information

int mode = -1;
try {
    mode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE); //this will return integer (0 or 1)
} catch (Exception e) {}

So, if you want to change brightness manually, you are supposed to set the manual mode first and after that you can change the brightness.

note: SCREEN_BRIGHTNESS_MODE_AUTOMATIC is 1

note: SCREEN_BRIGHTNESS_MODE_MANUAL is 0

You should use this

if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
    //Automatic mode
} else {
    //Manual mode
}

instead of this

if (mode == 1) {
    //Automatic mode
} else {
    //Manual mode
}

Now you can change the brightness manually

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);  //brightness is an integer variable (0-255), but dont use 0

and read brightness

try {
    int brightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);  //returns integer value 0-255
} catch (Exception e) {}

Now everything is set properly, but... you can't see the change yet. You need one more thing to see the change! Refresh the screen... so do this:

    try {
        int br = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);  //this will get the information you have just set...

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = (float) br / 255; //...and put it here
        getWindow().setAttributes(lp);
    } catch (Exception e) {}

Don't forget the permission...

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Tolkan answered 15/3, 2016 at 2:23 Comment(1)
+1 for needing to adjust screen_brightness mode to manual. Also, for API 23 and above, setting the permissions in manifest no longer works - see developer.android.com/training/permissions/requesting.html. See #32266925 for how to change the permissions for API 23 and above.Vina
S
8

Passing the context of the activity while setting up the parameters would also get the job done without any need to start another activity. The following worked for me-

WindowManager.LayoutParams lp = this.getWindow().getAttributes();
lp.screenBrightness =0.00001f;// i needed to dim the display
this.getWindow().setAttributes(lp);

i had this piece of code inside onSensorChanged() method which dimmed the display whenever it was triggered.

Sauter answered 21/5, 2014 at 14:40 Comment(2)
Never set it to zero. Set it to 0.00001f. Setting it to zero will cause a few some phones to turn off their screen indefinitely, forcing you to reset the phone by holding down the power button or removing the battery.Sessoms
Thanks, modified the answer.Sauter
T
4

COMPLEX EXAMPLE:

    try {
        //sets manual mode and brightnes 255
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);  //this will set the manual mode (set the automatic mode off)
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255);  //this will set the brightness to maximum (255)

        //refreshes the screen
        int br = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = (float) br / 255;
        getWindow().setAttributes(lp);

    } catch (Exception e) {}
Tolkan answered 15/3, 2016 at 2:34 Comment(2)
<uses-permission android:name="android.permission.WRITE_SETTINGS" />Tolkan
Don't forget the permission! Or nothing's gonna happen!Tolkan

© 2022 - 2024 — McMap. All rights reserved.