android - Setting the screen brightness to maximum level
Asked Answered
F

4

9

I'm a newbie to this field of android development. These days i'm developing an app and I want to set the screen brightness to maximum level once I open the app, and set it back to the previous level once I exit the app. Can someone come up with the full source code for this? I read almost every thread in stackoverflow regarding this issue. And I couldn't understand where to put those suggested codes. But if u can come up with the full code, then I'll be able to understand everything. Thanks!

Flatware answered 26/4, 2017 at 17:14 Comment(0)
A
5

You can use this

public class MainActivity extends AppCompatActivity {

private int brightness=255;
//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;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    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 (Settings.SettingNotFoundException e)
    {
        //Throw an error case it couldn't be retrieved
        Log.e("Error", "Cannot access system brightness");
        e.printStackTrace();
    }

    Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
    //Get the current window attributes
    WindowManager.LayoutParams layoutpars = window.getAttributes();
    //Set the brightness of this window
    layoutpars.screenBrightness = brightness / (float)100;
    //Apply attribute changes to this window
    window.setAttributes(layoutpars);
 }
}

And the most important permission in the manifest

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

Asking for Write Settings permission in API 23

private boolean checkSystemWritePermission() {
boolean retVal = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    retVal = Settings.System.canWrite(this);
    Log.d(TAG, "Can Write Settings: " + retVal);
    if(retVal){
        Toast.makeText(this, "Write allowed :-)", Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(this, "Write not allowed :-(", Toast.LENGTH_LONG).show();
       Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
       intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
       startActivity(intent);
    }
  }
 return retVal;
}
Antheridium answered 26/4, 2017 at 17:19 Comment(9)
if you don't mind, can you please paste the whole source code (from top to bottom) here as it is in Mainactivity.java. so that i can figure out what are the changes and where I need to make the changes in my code. (I do understand the manifest thing. so u dont need to paste that code again here.) Thank You!Flatware
I would have to implement it , waitAntheridium
hey m editing my code and pasting complete code here its workingAntheridium
It worked for me! Thanks alot. But there's another issue. It does work for my API 15 phone. but it doesn't work for my API 23 phone. What might be the reason for this?Flatware
@SankhaRathnayake the issue is starting API 23 you have to explicitly allow permissions , for this goto Settings->Apps or App management->Modifly System Settings then chechk for your app and allow permissionAntheridium
phonearena.com/news/… Check this , from Android 6.0 runtime permissions are introducedAntheridium
@SankhaRathnayake I have Edited my answer check the updated answer it has code if asking for permissionAntheridium
Where do I EXACTLY need to put that newly added "asking for permissions" code?Flatware
Anywhere, before you execute your code of changing brightness. for example if you have any other activity which comes before main activity.Antheridium
F
14

One of my friends sent me a better, simple code to fix this issue which he has found from the internet

For amateurs (like me), you have to enter below code after "setContentView(R.layout.activity_main);" in "protected void onCreate(Bundle savedInstanceState) {" method in your MainActivity.java

    WindowManager.LayoutParams layout = getWindow().getAttributes();
    layout.screenBrightness = 1F;
    getWindow().setAttributes(layout);

Btw, Thank you @AbdulKawee for your time and the support that u gave me with your code. really appreciate it :)

Flatware answered 27/4, 2017 at 6:55 Comment(3)
Worked perfectly. This looks like way to go if you only need it for this one activity. The method provided in the accepted answer is needed if you want to change brightness for other apps, too.Dillie
is there anyway to go back to automatic brightness in that activity?Madeline
I wanted was setting the brightness to the maximum for my app and restoring the brightness when exiting. At first, I tried to change system screen brightness which required special permissions (Android Studio said only system apps can use it). I almost gave it up, but then I remembered video player apps like VLC can control screen brightness during playback, and found this. This worked without permissions and it restored the brightness when exiting my app. Exactly what I wanted.Pappose
A
5

You can use this

public class MainActivity extends AppCompatActivity {

private int brightness=255;
//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;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    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 (Settings.SettingNotFoundException e)
    {
        //Throw an error case it couldn't be retrieved
        Log.e("Error", "Cannot access system brightness");
        e.printStackTrace();
    }

    Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
    //Get the current window attributes
    WindowManager.LayoutParams layoutpars = window.getAttributes();
    //Set the brightness of this window
    layoutpars.screenBrightness = brightness / (float)100;
    //Apply attribute changes to this window
    window.setAttributes(layoutpars);
 }
}

And the most important permission in the manifest

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

Asking for Write Settings permission in API 23

private boolean checkSystemWritePermission() {
boolean retVal = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    retVal = Settings.System.canWrite(this);
    Log.d(TAG, "Can Write Settings: " + retVal);
    if(retVal){
        Toast.makeText(this, "Write allowed :-)", Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(this, "Write not allowed :-(", Toast.LENGTH_LONG).show();
       Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
       intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
       startActivity(intent);
    }
  }
 return retVal;
}
Antheridium answered 26/4, 2017 at 17:19 Comment(9)
if you don't mind, can you please paste the whole source code (from top to bottom) here as it is in Mainactivity.java. so that i can figure out what are the changes and where I need to make the changes in my code. (I do understand the manifest thing. so u dont need to paste that code again here.) Thank You!Flatware
I would have to implement it , waitAntheridium
hey m editing my code and pasting complete code here its workingAntheridium
It worked for me! Thanks alot. But there's another issue. It does work for my API 15 phone. but it doesn't work for my API 23 phone. What might be the reason for this?Flatware
@SankhaRathnayake the issue is starting API 23 you have to explicitly allow permissions , for this goto Settings->Apps or App management->Modifly System Settings then chechk for your app and allow permissionAntheridium
phonearena.com/news/… Check this , from Android 6.0 runtime permissions are introducedAntheridium
@SankhaRathnayake I have Edited my answer check the updated answer it has code if asking for permissionAntheridium
Where do I EXACTLY need to put that newly added "asking for permissions" code?Flatware
Anywhere, before you execute your code of changing brightness. for example if you have any other activity which comes before main activity.Antheridium
E
3

Kotlin version of Sankha Rathnayake's answer:

val attributes = window.attributes
attributes.screenBrightness = 1f
window.attributes = attributes
Etsukoetta answered 30/5, 2021 at 21:30 Comment(0)
P
0

If you only want it in your app, then every Activity that should be full brightness just needs to implement the code below.

Whenever you switch to regular Activities or switch back to other apps - the brightness will be user-defined again. The Activity on which the brightness was set programmatically will remember your setting though.

If you can implement the code in onCreate() then use following:

window.attributes.apply {
    screenBrightness = LayoutParams.BRIGHTNESS_OVERRIDE_FULL
}

In case you want to update the brightness at runtime of the Activity you need to additionally notify the system so that the changes can apply:

window.apply {
    attributes.apply {
        screenBrightness = LayoutParams.BRIGHTNESS_OVERRIDE_FULL
    }
    addFlags(LayoutParams.SCREEN_BRIGHTNESS_CHANGED)
}
Proteus answered 17/4, 2023 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.