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!
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;
}
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 :)
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;
}
Kotlin version of Sankha Rathnayake's answer:
val attributes = window.attributes
attributes.screenBrightness = 1f
window.attributes = attributes
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)
}
© 2022 - 2024 — McMap. All rights reserved.