如何在Android应用程序上自动切换到黑暗模式?

如何在Android应用程序上自动切换到黑暗模式?,android,android-studio,user-interface,android-dark-theme,Android,Android Studio,User Interface,Android Dark Theme,我正在制作一个Android应用程序。我为黑暗模式制作了另一个用户界面。这就是我需要的;应用程序将在当地时间自动切换到黑暗主题。例如,当当地时间太阳下山时,应用程序将切换到黑暗模式 或者另一种选择是在一天中预先设定的时间切换到黑暗模式。希望你能理解我的问题。如果有人知道,请帮助我,如果可能的话,我更喜欢第一种选择。提前谢谢。也许你可以看看 您只需使用DayNight的父项定义主题: <style name="MyTheme" parent="Theme.AppCompat.DayNight

我正在制作一个Android应用程序。我为黑暗模式制作了另一个用户界面。这就是我需要的;应用程序将在当地时间自动切换到黑暗主题。例如,当当地时间太阳下山时,应用程序将切换到黑暗模式


或者另一种选择是在一天中预先设定的时间切换到黑暗模式。希望你能理解我的问题。如果有人知道,请帮助我,如果可能的话,我更喜欢第一种选择。提前谢谢。

也许你可以看看

您只需使用DayNight的父项定义主题:

<style name="MyTheme" parent="Theme.AppCompat.DayNight">    
<!-- Blah blah -->
</style>
您通常会在自己的自定义应用程序类中执行此操作:

public class MyApplication extends Application {

    public void onCreate() {
        super.onCreate();        
        AppCompatDelegate.setDefaultNightMode(
            AppCompatDelegate.MODE_NIGHT_YES);
    }
}
更多信息

快捷方式:

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //restore preferences
        SharedPreferences settings0 = this.getSharedPreferences(PREFS_NAME, 0);
        lightMode = settings0.getBoolean("key0", true);

        //retrieve selected mode
        if (lightMode) {

            //light mode
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

        } else {

            //dark mode
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        }

        Switch switch0 = findViewById(R.id.Switch0);
        switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (darkMode) {

                    text = "Mode: light";

                    //light mode
                    getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);

                    darkMode = false;

                } else {

                    text = "Mode: dark";

                    //dark mode
                    getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);

                    darkMode = true;
                }

                //save music preferences
                SharedPreferences setting0 = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor0 = setting0.edit();
                editor0.putBoolean("key0", darkMode);
                editor0.apply();
            }
        });
}

结帐。。1> 2>实际上有一种更好的方法来制作暗模式。您可以创建一个开关来更改明暗模式的背景色。
MODE_NIGHT_NO. Always use the day (light) theme.
MODE_NIGHT_YES. Always use the night (dark) theme.
MODE_NIGHT_FOLLOW_SYSTEM (default). This setting follows the system’s setting, which on Android Q and above is a system setting (more on this below).
MODE_NIGHT_AUTO_BATTERY. Changes to dark when the device has its ‘Battery Saver’ feature enabled, light otherwise.
MODE_NIGHT_AUTO_TIME & MODE_NIGHT_AUTO. Changes between day/night based on the time of day.
public class MyApplication extends Application {

    public void onCreate() {
        super.onCreate();        
        AppCompatDelegate.setDefaultNightMode(
            AppCompatDelegate.MODE_NIGHT_YES);
    }
}
public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //restore preferences
        SharedPreferences settings0 = this.getSharedPreferences(PREFS_NAME, 0);
        lightMode = settings0.getBoolean("key0", true);

        //retrieve selected mode
        if (lightMode) {

            //light mode
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

        } else {

            //dark mode
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        }

        Switch switch0 = findViewById(R.id.Switch0);
        switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (darkMode) {

                    text = "Mode: light";

                    //light mode
                    getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);

                    darkMode = false;

                } else {

                    text = "Mode: dark";

                    //dark mode
                    getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);

                    darkMode = true;
                }

                //save music preferences
                SharedPreferences setting0 = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor0 = setting0.edit();
                editor0.putBoolean("key0", darkMode);
                editor0.apply();
            }
        });
}