如何设置屏幕亮度?

时间:2014-04-26 20:40:18

标签: android screen brightness

我想通过点击一个按钮调整屏幕亮度,所以当背景为白色时,屏幕亮度应该是最大的,同时如果背景是黑色,屏幕亮度应该是最小的,但是我得到一个错误:NullPointerException .. 。 这是我的代码:

public void lamp2(boolean mode){

        if(mode){

            r.setBackgroundColor(Color.WHITE);
            btn.setText("Turn OFF");
            btn.setTextColor(Color.RED);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = 90 / 100.0f;
            getWindow().setAttributes(lp);
            this.mode = true;
        }

        else if(!mode){

            r.setBackgroundColor(Color.BLACK);
            btn.setText("Turn ON");
            btn.setTextColor(Color.GREEN);
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = 100 / 100.0f;
            getWindow().setAttributes(lp);
            this.mode = false;
        }
    }

4 个答案:

答案 0 :(得分:6)

将这些行放到最大值

WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 1.0f;
getWindow().setAttributes(params);

将这些行放到最低

WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 0.1f;
getWindow().setAttributes(params);

答案 1 :(得分:0)

如果要管理活动的亮度,则必须在onResume(或onCreate)方法中使用以下代码:

此代码在调用时会更改屏幕的亮度,但不会更改系统的亮度参数(这是一种好习惯)。当您的活动被破坏(完成)时,亮度将恢复正常。无需在清单中请求许可,也无需在离开应用程序时重新设置亮度值。简要说明一下,您在管理屏幕亮度时可以使用此代码。

 override fun onCreate(savedInstanceState: Bundle?) {
        handleScreenBrightness()
        super.onCreate(savedInstanceState)


    }


    private fun handleScreenBrightness() {
        val params = window.attributes
        params.screenBrightness = 1.0f// 0.0 - 1.0
        window.attributes = params
        window.addFlags(WindowManager.LayoutParams.FLAGS_CHANGED)
    }

答案 2 :(得分:0)

在Android清单中添加Bright设置权限

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

“写入设置”是受保护的设置,因此要求用户允许“写入系统”设置:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.System.canWrite(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivity(intent);
            }
        }

现在您可以轻松设置亮度

            ContentResolver cResolver = getContentResolver();
            Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, 2);

答案 3 :(得分:0)

科特林:

    val lp = window.attributes
    lp.screenBrightness = 0.6F
    window.attributes = lp

具有权限的所有代码:


    private fun changeSystemBrightness(brightness: Float) {
        if (hasPermissionsToWriteSettings(this)) {
            val lp = window.attributes
            lp.screenBrightness = brightness
            window.attributes = lp
        } else {
            println("not allow permissions")
        }
    }

    private fun hasPermissionsToWriteSettings(context: Activity): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Settings.System.canWrite(context)
        } else {
            ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED
        }
    }