如何创建 "Automatic Brightness" 来根据阳光条件调整液晶照明?

How to create "Automatic Brightness" to adjust lcd lighting, depending on sunlight conditions?

我创建了一个 android 应用程序(销售点终端),安装在 户外 加油站。我有一个问题:应用程序在白天变得不可见(当阳光出现时)

根据here的信息,android应用程序在阳光出现时保持可见是将亮度设置为最大值。

如何创建 "Automatic Brightness" 来根据阳光条件调整 lcd 照明?

房间外,条件为:

  1. 早晨/阴天:屏幕亮度适中。
  2. 白天/晴天:高亮度屏幕。
  3. 夜间:屏幕亮度较低。

谢谢。

为了根据预定时间自动更改亮度,我创建了 thread worker,它每 1 小时自动 运行 adjustBrightness 函数区间.

// AutoBrightness.java

import android.app.Activity;
import android.content.Context;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManager;

import static android.provider.Settings.System.SCREEN_BRIGHTNESS;

public class AutoBrightness {
    private final static String TAG = AutoBrightness.class.getName();
    private static boolean _isBrightnessThreadEnable;
    private static BrightnessThread _brightnessThread;
    private static Context _context;
    private static AutoBrightness _instance;

    private AutoBrightness(Context context) {
        _context = context;
    }

    public static AutoBrightness getInstance(Context context) {
        if (_instance == null) {
            _instance = new AutoBrightness(context);
        }
        return _instance;
    }

    public void start() {
        if (!_isBrightnessThreadEnable) {
            _brightnessThread = new BrightnessThread(_context);
            _brightnessThread.start();
            _isBrightnessThreadEnable = true;
        }
    }

    private class BrightnessThread extends Thread {
        Context _context;

        BrightnessThread(Context context) {
            _context = context;
            Settings.System.putInt(_context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            setPriority(Thread.NORM_PRIORITY);
        }

        private long getCurrentTime() {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(SHORT_TIME_FORMAT);
            String currentTime = simpleDateFormat.format(Calendar.getInstance().getTime());
            try {
                return simpleDateFormat.parse(currentTime).getTime();
            } catch (ParseException ex) {
                Log.e(TAG, Log.getStackTraceString(ex));
            }
            return 0;
        }

        private void adjustBrightness(float backlightValue){
            WindowManager.LayoutParams layoutParams = ((Activity) _context).getWindow().getAttributes();
            layoutParams.screenBrightness = backlightValue;
            ((Activity) _context).getWindow().setAttributes(layoutParams);

            int systemBacklightValue = (int) (backlightValue * 255);
            android.provider.Settings.System.putInt(_context.getContentResolver(), SCREEN_BRIGHTNESS, systemBacklightValue);
        }

        @Override
        public void run() {
            long morningTime = -3600000L, dayTime = 10800000L, nightTime = 39600000L;
            float backlightValue = 0.5f;

            boolean isMorningAdjusted = false;
            boolean isDayAdjusted = false;
            boolean isNightAdjusted = false;

            while (_isBrightnessThreadEnable) {

                long currentTime = getCurrentTime();
                if (currentTime >= morningTime && currentTime < dayTime) {

                    // 06:00 - 09:59 (morning)
                    if (!isMorningAdjusted) {
                        backlightValue = 0.50f;
                        adjustBrightness(backlightValue);
                        isMorningAdjusted = true;

                        isDayAdjusted = false;
                        isNightAdjusted = false;
                    }

                } else if (currentTime >= dayTime && currentTime < nightTime) {

                    // 10:00 - 17:59 (day)
                    if (!isDayAdjusted) {
                        backlightValue = 1.00f;
                        adjustBrightness(backlightValue);
                        isDayAdjusted = true;

                        isMorningAdjusted = false;
                        isNightAdjusted = false;
                    }
                } else {

                    // 18:00 - 05:59 (night)
                    if (!isNightAdjusted) {
                        backlightValue = 0.35f;
                        adjustBrightness(backlightValue);
                        isNightAdjusted = true;

                        isMorningAdjusted = false;
                        isDayAdjusted = false;
                    }
                }

                try {
                    // delay for 1 hours
                    Thread.sleep(1000 * 60 * 60);
                } catch (InterruptedException ex) {
                    Log.e(TAG, Log.getStackTraceString(ex));
                }
            }
        }
    }
}

如何使用?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    _autoBrightness = AutoBrightness.getInstance(this);
    _autoBrightness.start();
}