[英]Android Adjusting System Brightness from a Service
I'm relatively new to Android development. 我是Android开发的新手。 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.
我认为这可能通过使用ViewOverlays或其他方法来实现,但是到目前为止我尝试过的所有方法都失败了。 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. 我需要一个解决方案,其中我的应用程序在后台启动服务,然后当您在其他应用程序或主屏幕中使用设备时,该服务会调整屏幕亮度。
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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.