Android change screen brightness

How to Maximize/Minimize Screen Brightness Programmatically in Android?

Screen brightness is one such factor that directly affects the users as well as the battery on a device. Android devices are Smart systems and have an inbuilt system for Auto-Brightness. But mostly this feature is unchecked by the users or set off by default. Irrespective of whether this feature is present, set on or off, or absent in any device, a developer must take this opportunity into consideration and develop an optimized application. Anything that is declared inside the application might have an effect on the outside space, i.e., if the screen brightness was changed programmatically from an application, the brightness value might stay unaltered even after exiting the application. So one must try to trace back the originals and set them before a user exits.

Where can we use this feature?

  1. Applications Streaming Videos: Each frame could be analyzed and compared with the ambient light of the room and accordingly make changes while viewing it to the users.
  2. Low Battery Situations: Brightness can be set at a low value if the battery level is low.
  3. If the screen is inactive or unresponded: If the screen is inactive or unresponded, the brightness could be lowered after a specific time-out.

A sample GIF is given below to get an idea about what we are going to do in this article . Note that we are going to implement this project using the Kotlin language.

Approach

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Читайте также:  Среда андроид для windows

Источник

Changing the Screen Brightness System Setting Android

I’m attempting to change the screen brightness from withing a service, like so:

Problem is that is doesn’t work. Well, actually it succeeds in changing the brightness setting, but the screen brightness doesn’t actually change till I go into the phones settings, look at the new value and hit Ok.

Is there something I have to do after setting the value to get the brightness to change?

4 Answers 4

I’ve had the same problem of changing screen brightness from within a service, and a couple days ago i have successfully solved it(and updated my app Phone Schedule with brightness feature 😉 ). Ok, so this is the code you put into your service:

Please Note that in the above code snippet I’m using two variables for brightness. One is brightness , which is a float number between 0.0 and 1.0, the other one is brightnessInt , which is an integer between 0 and 255. The reason for this is that Settings.System requires an integer to store system wide brightness value, while the lp.screenBrightness which you will see in the next code snippet requires a float. Don’t ask me why not use the same value, this is just the way it is in Android SDK, so we’re just going to have to live with it.

This is the code for DummyBrightnessActivity:

This is how you add your activity to the AndroidManifest.xml, probably the most important part:

A little explanation about what’s what.

android:taskAffinity must be different, than your package name! It makes DummyBrightnessActivity be started not in your main stack of activities, but in a separate, which means that when DummyBrightnessActivity is closed, you won’t see the next activity, whatever that may be. Until i included this line, closing DummyBrightnessActivity would bring up my main activity.

android:excludeFromRecents=»true» makes this activity not available in the list of recently launched apps, which you definetely want.

Читайте также:  Фонарик для телефонов android

android:theme=»@style/EmptyActivity» defines the way DummyBrightnessActivity looks like to the user, and this is where you make it invisible. This is how you define this style in the styles.xml file:

This way your DummyBrightnessActivity will be invisible to the user. I’m not shure if all of those style parameters are really necessary, but it works for me this way.

I hope that explains it, but if you have any questions, just let me know.

Источник

Change the System Brightness Programmatically

I want to change the system brightness programmatically. For that purpose I am using this code:

because I heard that max value is 255.

but it does nothing. Please suggest any thing that can change the brightness. Thanks

16 Answers 16

You can use following:

In your onCreate write:

Write the code to monitor the change in brightness.

then you can set the updated brightness as follows:

Permission in manifest:

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

I had the same problem.

here, brightness = (int) 0 to 100 range as i am using progressbar

1 SOLUTION

2 SOLUTION

I just used dummy activity to call when my progress bar stop seeking.

Now coming to the DummyBrightnessActivity.class

don’t forget to register DummyBrightnessActivity to manifest.

In my case, I only want to light up the screen when I display a Fragment and not change the system wide settings. There is a way to only change the brightness for your Application/Activity/Fragment. I use a LifecycleObserver to adjust the screen brightness for one Fragment :

And add the LifecycleObserver such as this in your Fragment :

I tried several solutions that others posted and none of them worked exactly right. The answer from geet is basically correct but has some syntactic errors. I created and used the following function in my application and it worked great. Note this specifically changes the system brightness as asked in the original question.

Читайте также:  Com android json что это

Complete Answer

I did not wanted to use Window Manager to set brightness. I wanted the brighness to reflect on System level as well as on UI. None of the above answer worked for me. Finally this approach worked for me.

Add Write setting permission in Android Manifest

Write Settings is a Protected settings so request user to allow Writing System settings:

Now you can set Brightness easily

brighness value should be in range of 0-255 so if you have aslider with range (0-max) than you can normalize the value in range of (0-255)

Finally you can now change Brightness in of 0-100% from 0-255 range like this:

Hope it will save your time.

this worked for me till kitkat 4.4 but not in android L

This worked for me. No need of a dummy activity. This works only for your current activity.

This is the complete code on how to change system brightness

Or you may check this tutorial for complete code
happy coding:)

Add this in manifest

Please Try this , it’s May help you. Worked fine for me

According to my experience

where the brightness value very according to 1.0f.100f is maximum brightness.

The above mentioned code will increase the brightness of the current window. If we want to increase the brightness of the entire android device this code is not enough, for that we need to use

Where 192 is the brightness value which very from 1 to 255. The main problem of using 2nd method is it will show the brightness in increased form in android device but actually it will fail to increase android device brightness.This is because it need some refreshing.

That is why I find out the solution by using both codes together.

Источник

Оцените статью