[英]How to change the theme for an Android app based on the ambiental light?
I am trying to develop an Android app (this is a school project and I do not have experience, but I would like to learn) and I need to change the theme based on the ambiental light.我正在尝试开发一个 Android 应用程序(这是一个学校项目,我没有经验,但我想学习),我需要根据环境光更改主题。
About the project : I have an app which is used to keep the information about the books that the user is reading (open books, finished books, abandoned books, reading sessions, etc.).关于项目:我有一个应用程序,用于保存有关用户正在阅读的书籍的信息(打开的书籍、完成的书籍、废弃的书籍、阅读会话等)。 The idea is to change the theme based on the ambiental light.
这个想法是根据环境光改变主题。
This is the code used in order to detect the light value using the ambiental light sensor:这是用于使用环境光传感器检测光值的代码:
private Toast mToastToShow;
private SensorManager sensorManager;
private Sensor lightSensor;
private SensorEventListener lightSensorEventListener;
private float maxValue;
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
lightSensor = sensorManager.getDefaultSensor(TYPE_LIGHT);
if(lightSensor == null){
Toast.makeText(this,"Oh, snap... This device has not a light sensor", Toast.LENGTH_SHORT).show();
finish();
}
maxValue = lightSensor.getMaximumRange();
lightSensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
changeTheme(event);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d(this.getClass().getName(), "The accuracy of the sensor has changed");
}
};
}
public void showToast(String message) {
int toastDurationInMilliSeconds = 50000;
mToastToShow = Toast.makeText(this, message, Toast.LENGTH_LONG);
CountDownTimer toastCountDown;
toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, 1000 /*Tick duration*/) {
public void onTick(long millisUntilFinished) {
mToastToShow.show();
}
public void onFinish() {
mToastToShow.cancel();
}
};
mToastToShow.show();
toastCountDown.start();
}
@Override
protected void onStart(){
super.onStart();
sensorManager.registerListener(lightSensorEventListener, lightSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onResume(){
super.onResume();
sensorManager.registerListener(lightSensorEventListener, lightSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(lightSensorEventListener);
}
@Override
protected void onStop() {
super.onStop();
sensorManager.unregisterListener(lightSensorEventListener);
}
private void changeTheme(SensorEvent event){
float value = event.values[0];
if(value >= 14379) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}else{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
}
The only problem that I can not resolve is that when the app starts, I do not know how to get the light value without changing the ambiental light (using the virtual sensors from the emulator).我无法解决的唯一问题是,当应用程序启动时,我不知道如何在不改变环境光的情况下获取光值(使用模拟器中的虚拟传感器)。 The problem is that the light value is only retrieved when the on sensor changed event is triggered.
问题是只有在触发传感器更改事件时才会检索光值。
To be specific, the scenario is the following:具体来说,场景如下:
Could you please tell me if there is a solution for this problem or if I have made a mistake (is this a normal behavior)?您能否告诉我是否有解决此问题的方法或者我是否犯了错误(这是正常行为)?
Thank you for your time!感谢您的时间!
After some research, turns out that you are not able to do this thing because the light sensor is a hardware one and you cannot try to trigger the on change event.经过一番研究,事实证明您无法执行此操作,因为光传感器是硬件传感器,您无法尝试触发 on change 事件。 But on a physical device, it works (because the ambiental light is not always constant) and the only problem is when you try to simulate this behavior on the emulator.
但是在物理设备上,它可以工作(因为环境光并不总是恒定的),唯一的问题是当您尝试在模拟器上模拟这种行为时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.