我创建了一个Android应用程序(销售点终端),安装在加油站的户外。我有一个问题:应用程序在白天(当阳光出现时)变得不可见。
根据here的信息,当出现太阳光时,Android应用程序仍然可见,将亮度设置为最大值。
如何创建"自动亮度"根据阳光条件调整lcd照明?
在房间外面,条件是:
感谢。
答案 0 :(得分:0)
要根据预定时间自动更改亮度,我会创建线程工作者,每隔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();
}