编程改变屏幕亮度(与电源部件) [英] Changing screen brightness programmatically (as with the power widget)

查看:110
本文介绍了编程改变屏幕亮度(与电源部件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找如何以编程方式改变屏幕的亮度,我发现<一href="http://stackoverflow.com/questions/3737579/changing-screen-brightness-programmatically-in-android">this这是非常好的解决方案,它的作品不错,但它只有在我的应用程序被激活。经过我的应用程序被关闭则亮度被返回的值相同之前,我开始我的应用程序。

我希望能够改变亮度就像当我$从我的力量插件的亮度按钮p $ PSS。在来自Android的电源部件有3种状态。一个非常光明的很暗,一个在两者之间。是否有可能改变亮度,当有人$这个小部件p $ PSS一样?

EDIT1: 我用200,但没有改变创造了这个和我说permision到我的清单,但是,当应用程序开始我没有看到任何改变亮度,我试着用10,100,现在 有什么建议?

 公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
android.provider.Settings.System.putInt(this.getContentResolver(),
        android.provider.Settings.System.SCREEN_BRIGHTNESS,200);
}
 

解决方案

这是一个完整的code,我发现是工作的:

  Settings.System.putInt(this.getContentResolver()
        Settings.System.SCREEN_BRIGHTNESS,20);

WindowManager.LayoutParams LP = getWindow()的getAttributes()。
lp.screenBrightness = 0.2F; // 100 / 100.0f;
。getWindow()setAttributes(LP);

startActivity(新意图(这一点,RefreshScreen.class));
 

这是我的问题的code不起作用,因为屏幕没有得到刷新。所以有黑客在刷新屏幕开始虚活动,比上创建的虚拟活动叫完成()以免因亮度的变化生效。

I was searching how to change the brightness of the screen programmatically and I found this it is very good solution and it works nice, but it works only while my app is active. After my application is shutdown then the brightness is returned back the the same value as before I start my app.

I want to be able to change the brightness just like when I press on the brightness button from my power widget. In the power widget that comes from android there are 3 states. One very bright one very dark and one in between. Is it possible to change the brightness just like when someone press on this widget ?

Edit1: I created this and I added permision to my manifest but when the app is started I do not see any changes to the brightness, I tried with 10 with 100 and now with 200 but no changes any suggestions ?

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.provider.Settings.System.putInt(this.getContentResolver(),
        android.provider.Settings.System.SCREEN_BRIGHTNESS, 200);
}

解决方案

This is the complete code I found to be working:

Settings.System.putInt(this.getContentResolver(),
        Settings.System.SCREEN_BRIGHTNESS, 20);

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness =0.2f;// 100 / 100.0f;
getWindow().setAttributes(lp);

startActivity(new Intent(this,RefreshScreen.class));

The code from my question does not work because the screen doesn't get refreshed. So one hack on refreshing the screen is starting dummy activity and than in on create of that dummy activity to call finish() so the changes of the brightness take effect.

这篇关于编程改变屏幕亮度(与电源部件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆