2011-09-26 78 views
3

我想设置屏幕亮度,但是当我尝试使用this.getWindow()获取当前窗口时,我得到空值。为什么是这样?我将在我的setBrightness()方法中发布我的所有代码。试图在Android手机上设置屏幕亮度

System.putInt(getContentResolver(), System.SCREEN_BRIGHTNESS, 
      brightness); 
Window window = getWindow(); 
WindowManager.LayoutParams lp = window.getAttributes(); 
lp.screenBrightness = brightness/(float) 255; 
window.setAttributes(lp); 
+0

你是从'OnClickListener()'或类似的东西调用'getWindow()'吗? –

+0

不只是一项活动。该活动不可见,并且该方法从另一个接收来自主活动类的通知的类中调用。我真的希望它只是普通的类,但它似乎必须是一个活动,所以它可以访问getWindow()方法? – plast

+0

这里是简单的方法来做到这一点希望它可以帮助你http://stackoverflow.com/questions/2937365/increasing-screen-brightness-for-activity/21829712#21829712 –

回答

0

请勿使用System.putInt()...您已经在lp中设置了screenBrightness!

下面的代码工作:

Window window = getWindow(); 
float brightness = 100.0f; 
WindowManager.LayoutParams lp = window.getAttributes(); 
lp.screenBrightness = brightness/255.0f; 
window.setAttributes(lp); 
+0

感谢您的输入!尽管getWindow()仍然是null。也许我以某种方式检索了错误的窗口? – plast

+0

验证你的进口我认为你应该导入android.view.Window – Houcine

+0

这些是我的视图导入: import android.view.Window; import android.view.WindowManager;当你调用getwindow()方法时,调用方法是 – plast

0

如果您使用的搜索条,请尝试以下线,

System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness); 
LayoutParams layoutpars = window.getAttributes(); 
layoutpars.screenBrightness = brightness/(float)255; 
window.setAttributes(layoutpars); 
0

如果从片段把它添加getWindow前getActivity()()这样的

getActivity().getWindow().setAttributes(layoutParams); 

如果使用seekbar不要让你的brogress为0,因为它可以使你的屏幕变得完整(在android 2.3上)