简体   繁体   English

如何根据环境光更改 Android 应用程序的主题?

[英]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:具体来说,场景如下:

  1. I need to have the dark theme on when the light value is not greater or equal than 14379当光值不大于或等于 14379 时,我需要打开深色主题
  2. If the light value is greater or equal than 14379, then the light theme must be applied.如果灯光值大于或等于 14379,则必须应用灯光主题。
  3. When the app starts, the theme is not updating because the on change event for the sensor is not triggered.当应用程序启动时,主题不会更新,因为传感器的 on change 事件没有被触发。
  4. For example, when the app starts, if the light value is smaller than the limit, the dark theme must be applied, but because the on change event is not triggered, the light theme is applied.例如,当应用启动时,如果 light 值小于限制,则必须应用深色主题,但由于未触发 on change 事件,因此应用了浅色主题。

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.

相关问题 在Android应用中实现更改黑/亮主题功能 - Implement change black/light theme feature in Android app 如何更改工具栏searchView下拉菜单主题,使其在android应用中轻巧 - how can I change toolbar searchView drop down menu theme to be light in android app 将android:Theme.Light更改为Theme.AppCompat.Light.DarkActionBar - change android:Theme.Light to Theme.AppCompat.Light.DarkActionBar 如何根据 android(暗/亮主题)中的当前应用主题从字符串名称获取可绘制资源 ID - How to get drawable resource id from string name base on current app theme in android (dark/light theme) 如何在钛金中使用Android的灯光主题 - How to use light theme for android in titanium 如何使用switchCompat更改app android主题 - How to change app android theme with switchCompat 如何在运行时更改 android 应用程序主题? - How to Change android app theme in runtime? 如何更改android app的颜色主题? - How to change the color theme of android app? 如何将TabHost的主题从Holo.Light更改为Dark主题 - How to change theme of TabHost from Holo.Light to Dark theme Android:如何在Theme.Holo.Light.Dialog.MinWidth中更改DatePickerDialog颜色? - Android: How to change DatePickerDialog colors in Theme.Holo.Light.Dialog.MinWidth?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM