简体   繁体   English

在 Android 活动中设置全屏亮度

[英]Setting full screen brightness in an Android activity

I'm using this method to set the screen to full brightness.我正在使用这种方法将屏幕设置为全亮度。

@SuppressLint("NewApi") 
private void setFullBright() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
        WindowManager.LayoutParams windowParams = getWindow().getAttributes();
        windowParams.screenBrightness = 1.0f;
        getWindow().setAttributes(windowParams);        
    }
}

If I want the full brightness to be set on the entire life of the Activity's screen, is the onCreate method the best place to call it?如果我想在 Activity 屏幕的整个生命周期中设置全亮度,那么 onCreate 方法是调用它的最佳位置吗?

Is there an XML flag that can achieve this?是否有可以实现此目的的 XML 标志? Something like android:keepScreenOn="true" that mirrors the functionality of adding WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON in code?android:keepScreenOn="true"这样的东西反映了在代码中添加WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON的功能?

Put these lines in the oncreate method of all java files which are used to view pages,将这些行放在所有用于查看页面的 java 文件的 oncreate 方法中,

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

This will solve your problem, Happy coding...这将解决您的问题,快乐编码...

For everyone who's trying to achieve the same in a DialogFragment .对于试图在DialogFragment实现相同目标的每个人。 Applying the params to getActivity().getWindow() won't help since the window of the Activity is not the same as the window the Dialog is running in. So you have to use the window of the dialog - see following snippet:将参数应用到getActivity().getWindow()将无济于事,因为Activity的窗口与运行Dialog的窗口不同。因此您必须使用对话框的窗口 - 请参阅以下代码段:

getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL;
getDialog().getWindow().setAttributes(params);

And to answer the original question: No there is no way to set this via XML.并回答最初的问题:不,无法通过 XML 进行设置。

Kotlin version with constant instead of float: (not for Dialogs) Kotlin 版本使用常量而不是浮点数:(不适用于对话框)

private fun setScreenBright() {
    with(window){
        addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        attributes = attributes.also { 
            it.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM