Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - Setting the screen brightness to maximum level

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!

like image 693
Sankha Rathnayake Avatar asked Apr 26 '17 17:04

Sankha Rathnayake


People also ask

How can I make my Android brightness higher than Max?

📲 On Android: Settings > Display > Tap the slider next to Adaptive brightness and switch it to the off position. Then, adjust the brightness bar until you've reached your desired level of brightness.

How can I increase the brightness of my phone above maximum?

Adjust brightness on Android device On an unlocked device, swipe your finger down from the top of the screen twice. Press and hold your finger on the brightness slider (shown below) and drag left or right to adjust the brightness.

Why is my phone screen so dark on full brightness Android?

If you notice that your phone's screen gets dark and then brightens, it's due to the Adaptive brightness feature which changes the screen's brightness based on the amount of light detected. This feature is designed to help save battery power, but you can shut it off if it bothers you.


3 Answers

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 :)

like image 78
Sankha Rathnayake Avatar answered Oct 01 '22 00:10

Sankha Rathnayake


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;
}
like image 27
Abdul Kawee Avatar answered Oct 01 '22 00:10

Abdul Kawee


Kotlin version of Sankha Rathnayake's answer:

val attributes = window.attributes
attributes.screenBrightness = 1f
window.attributes = attributes
like image 37
Skoua Avatar answered Sep 30 '22 22:09

Skoua