在android中以编程方式更改屏幕亮度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3737579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
changing screen brightness programmatically in android
提问by Sri Sri
I want to change the screen brightness programmatically in android. At the moment I use this code:
我想在 android 中以编程方式更改屏幕亮度。目前我使用这个代码:
WindowManager.LayoutParams lp = getWindow().getAttributes();
float brightness=1.0f;
lp.screenBrightness = brightness;
getWindow().setAttributes(lp);
But this sample code works on cupcake, not on latest versions. I am using the latest version of SDK.. What is the preferred solution for newer Android Versions?
但是此示例代码适用于cupcake,而不适用于最新版本。我使用的是最新版本的 SDK。较新的 Android 版本的首选解决方案是什么?
采纳答案by Maurits Rijk
回答by Tor-Morten
This is possible to do by using:
这可以通过使用:
WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 1F;
getWindow().setAttributes(layout);
See also: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness
另见:http: //developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness
回答by AZ_
You have to add params to Window
before it is created otherwise it will throw java.lang.IllegalArgumentException: Window type can not be changed after the window is added.
您必须Window
在创建之前添加参数,否则它会抛出java.lang.IllegalArgumentException: Window type can not be changed after the window is added.
See the example with a android.app.Dialog.Dialog
.
请参阅带有android.app.Dialog.Dialog
.
final Dialog dialog = new Dialog(this) {
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
WindowManager.LayoutParams layout = getWindow()
.getAttributes();
layout.screenBrightness = 1F;
getWindow().setAttributes(layout);
}
};
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.show();
Please note that brightness value is between 0.0F and 1.0F.
请注意,亮度值介于 0.0F 和 1.0F 之间。
回答by hungtdo
@kamal_tech_view: You must convert value layout.screenBrightness = value;to float
@kamal_tech_view:您必须转换值layout.screenBrightness = value; 漂浮
回答by Bhavesh Hirpara
Too late answer but want to improve..
回答太晚了,但想改进..
I tried with Tor-morten's code but it is for particular screen itself, I wanted to change anywhere, I made service for that.
我尝试使用 Tor-morten 的代码,但它是针对特定屏幕本身的,我想在任何地方进行更改,为此我提供了服务。
Change brightness according to surrounding light in android
Hope, It will be useful to others.
希望,这对其他人有用。
回答by Jayesh Kalkani
final Dialog dialog = new Dialog(act);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog
.setContentView(R.layout.menubase_brightness_control);
dialog.setCanceledOnTouchOutside(true);
SeekBar global_brightness_control = (SeekBar) dialog
.findViewById(R.id.global_brightness_control);
global_brightness_control.setMax(255);
global_brightness_control.setProgress(Settings.System.getInt(
con.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS));
global_brightness_control
.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
Settings.System
.putInt(con.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, progress);
}
});
dialog.show();