[英]changing screen brightness programmatically in android
I want to change the screen brightness programmatically in android. 我想在android中以编程方式更改屏幕亮度。 At the moment I use this code:
目前我使用此代码:
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?
我使用的是最新版本的SDK。对于较新的Android版本,首选解决方案是什么?
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
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.
您必须在创建之前将params添加到
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之间。
@kamal_tech_view: You must convert value layout.screenBrightness = value; @kamal_tech_view:你必须转换值layout.screenBrightness = value; to float
漂浮
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 根据android中的周围光线改变亮度
Hope, It will be useful to others. 希望,对其他人有用。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.