admin
2020-07-14 7af22bf20c862c8ab2270cfeef8f3530f174ac9f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.wpc.library.videocomponents;
 
import android.app.Activity;
import android.content.ContentResolver;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.Window;
import android.view.WindowManager;
 
public class ScreenBrightnessUtil {
    
    /**
     * 获取当前Activity亮度
     * 
     * @return A value of less than 0, the default, means to use the preferred screen brightness. 0 to 1 adjusts the brightness from dark to full bright.
     */
    public static float getBrightness(Activity activity) {
        Window window = activity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        return lp.screenBrightness;
    }
 
    /**
     * 设置当前Activity亮度
     * 
     * @param activity 当前activity
     * @param brightness A value of less than 0, the default, means to use the preferred screen brightness. 0 to 1 adjusts the brightness from dark to full bright.
     */
    public static void setBrightness(Activity activity, float brightness) {
        Window window = activity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.screenBrightness = brightness;
        window.setAttributes(lp);
    }
    
    /**
     * 设置当前Activity亮度
     * 
     * @param activity 当前activity
     * @param percentage 百分比(0~100)
     */
    public static void setBrightness(Activity activity, int percentage) {
        float brightness = percentage / 100f;
        setBrightness(activity, brightness);
    }
    
    /**
     * 获取屏幕的亮度
     * 
     * @return The screen backlight brightness between 0 and 255.
     */
    public static int getSystemBrightness(ContentResolver contentResolver) {
        int nowBrightnessValue = 0;
        try {
            nowBrightnessValue = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nowBrightnessValue;
    }
    
    /**
     * 设置系统亮度,如果开启了自动调节功能了,将没有效果
     * 
     * @param brightness The screen backlight brightness between 0 and 255.
     */
    public static void setSystemBrightness(ContentResolver contentResolver, int brightness) {
        Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
    }
    
    /**
     * 判断是否开启了自动亮度调节
     * 
     */
    public static boolean isAutoBrightness(ContentResolver contentResolver) {
        boolean automicBrightness = false;
        try {
            automicBrightness = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE)
                    == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        } catch (SettingNotFoundException e) {
            e.printStackTrace();
        }
        return automicBrightness;
    }
    
    /**
     * 停止亮度自动调节 
     * 
     */
    public static void stopAutoBrightness(ContentResolver contentResolver) {
        Settings.System.putInt(contentResolver,
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }
 
    /**
     * 开启亮度自动调节 
     * 
     */
    public static void startAutoBrightness(ContentResolver contentResolver) {
        Settings.System.putInt(contentResolver,
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    }
}