通过Android应用改变屏幕亮度 [英] changing screen brightness via android app

查看:662
本文介绍了通过Android应用改变屏幕亮度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样同样需要<一个href=\"http://stackoverflow.com/questions/7646865/changing-screen-brightness-programmatically-as-with-the-power-widget\">this,但我发现根据该问题的答案是不行的。

I have the same need like this, but I found the answer under that question aren't work.

我要让按钮我的应用程序运行时改变我的屏幕亮度。

I want to make buttons to change my screen brightness while my app is running.

我发现这code,但是如果我直接复制此code到我mainActivity这是行不通的。

I have found this code, but it doesn't work if I copy this code into my mainActivity directly.

WindowManager.LayoutParams lp = getWindow().getAttributes();
float brightness=1.0f;
lp.screenBrightness = brightness;
getWindow().setAttributes(lp);

我使用Android的工作室和API级别21
我添加的用户权限。

I use android studio and API level is 21 and I added user permission.

这件code是最接近我的目标,谁可以帮我运行此code?

This piece code is the nearest to my target, who can help me run this code?

推荐答案

这为我工作:

的onCreate()方法写这篇文章,

private int brightness;

try{
    Settings.System.putInt(getContentResolver(),
                           Settings.System.SCREEN_BRIGHTNESS_MODE,
                           Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);

    brightness = System.getInt(getContentResolver(), 
                               Settings.System.SCREEN_BRIGHTNESS);
}
catch(SettingNotFoundException e){
    Log.e("Error", "Cannot access system brightness");
    e.printStackTrace();
}

要更新的亮度,

System.putInt(getContentResolver(), System.SCREEN_BRIGHTNESS, brightness);
LayoutParams layoutpars = getWindow().getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
getWindow().setAttributes(layoutpars);

在权限清单,

<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>

这篇关于通过Android应用改变屏幕亮度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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