Reputation: 1156
I'm relatively new to Android development. I've been able to adjust screen brightness of my own app by doing things like this:
WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 0;
getWindow().setAttributes(layout);
What I really need to do though is change the screen brightness on a system level. I'd like to do this from a Service or Intent Service. The issue I'm running into is that
getWindow()
fails when you call it from a Service. I think this may be possible through the use of ViewOverlays or something but everything I've tried so far has failed. Does anyone have an idea?
I need a solution where my app starts a service in the background and then that service adjusts the screen brightness as you use the device in other apps or on the home screen.
Upvotes: 0
Views: 388
Reputation: 5339
You can try this :
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
value);
add this to manifest
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
and check for run-time permission like this
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_SETTINGS}, 1);
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission accepted
} else {
// permission denied,
}
return;
}
}
}
Upvotes: 1