即使在杀死应用程序后,如何保持黑暗模式?

发布于 2025-01-23 10:56:58 字数 2059 浏览 0 评论 0原文

这是我在stay.java类中的代码。我已经实现了这样的设置课。我想在杀死该应用程序的情况下以深色为主题,也必须在那里存在,并且必须遵守切换。

现在我已经成就了黑暗。但是当我杀死该应用程序并再次来时,它消失了。切换也可以关闭。

public class settings extends AppCompatActivity {
    SwitchCompat switchCompat;
    ImageView backbtn;
    Button button;    
    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
            setTheme(R.style.Base_ThemeOverlay_AppCompat_Dark);
        } else {
            setTheme(R.style.Base_Theme_MaterialComponents_Light);
        }
            backbtn = findViewById(R.id.goingback);
        LoadingDialod loadingDialod = new LoadingDialod(settings.this);

        backbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
            }
        });
        switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    loadingDialod.startloadinganimation();

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {                             AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                            Intent intent = new Intent(getApplicationContext(), settings.class);
                            startActivity(intent);
                        }
                    }, 800);

                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

                }
            }
        });
    }
}

This is my code in settings.java class. I have implemented my settings class like this. I want to make dark theme on and after killing the app it has to be present there as well and toggle has to be trued on.

Now I have achieved to make it dark. But when I kill the app and come again it gets disappeared. Toggle is also get turns off.

public class settings extends AppCompatActivity {
    SwitchCompat switchCompat;
    ImageView backbtn;
    Button button;    
    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
            setTheme(R.style.Base_ThemeOverlay_AppCompat_Dark);
        } else {
            setTheme(R.style.Base_Theme_MaterialComponents_Light);
        }
            backbtn = findViewById(R.id.goingback);
        LoadingDialod loadingDialod = new LoadingDialod(settings.this);

        backbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
            }
        });
        switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    loadingDialod.startloadinganimation();

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {                             AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                            Intent intent = new Intent(getApplicationContext(), settings.class);
                            startActivity(intent);
                        }
                    }, 800);

                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

                }
            }
        });
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2025-01-30 10:56:58

我已经给出了有关此但是我也在这里提供:


  1. 创建一个名为preverences Manager

    的类

  2. 粘贴此代码的类

    粘贴此代码

I have already given an answer about this here but I am giving it here also:


  1. Create a class named Preferences Manager

  2. Paste this code in the class

????

public final SharedPreferences sharedPreferences;

public PreferenceManager(Context context){
    sharedPreferences=context.getSharedPreferences( "PREFS",Context.MODE_PRIVATE );
}
public void putBoolean(String key,Boolean value){
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean( key,value );
    editor.apply();
}
public Boolean getBoolean (String key,Boolean defaultValue){
    return sharedPreferences.getBoolean( key,defaultValue );
}

public void clear(){
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.clear();
    editor.apply();
}
  1. Then in the fragment make an object named preferences manager like this.

    PreferencesManager preferencesManager = new PreferencesManager(getActivity());

  2. Then add value to it like this

if night mode is set to on.

preferencesManager.putBoolean("NightMode",true);

if night mode is off

preferencesManager.putBoolean("NightMode",false);

and then later to check if night mode if on or off, use this code

if (preferencesManager.getBoolean("NightMode")){
    //night mode is on ,do some magic
}else {
    //night mode is off ,do some magic
}

Edit


Alternatively you can also use my library here to store the data. To store and retrieve, see the README.md file

晨曦慕雪 2025-01-30 10:56:58

该答案提供了Android的适应深夜模式。深色主题适用于Android 10(API级别29)且更高。这是一个先决条件。

对于API 29 -API 30

如果要在应用程序中全球启用Dark Night模式,则需要满足以下条件:

  1. 相应的活动应从AppCompatactivity继承。

  2. 在适当的位置调用以下代码,以在应用程序中启用全局暗模式。

      appcompatdelegate.setdefaultnightnightmode(appcompatdelegate.mode_night_yes)
     

对于API 31及以上的API

如果您想在应用程序中全球启用深夜模式,则需要满足以下条件:

  1. 获取UimodeManager。


    GetSystemService(UI_Mode_Service)作为UimodeManager
    }

  2. 在适当的位置调用以下代码,以在应用程序中启用全局暗模式。

      uimanager.setapplicationnightnightmode(uimodemanager.mode_night_yes)
     

This answer provides Android's adaptation to dark night mode. The dark theme is suitable for Android 10 (API level 29) and higher. This is a prerequisite.

For API 29 - API 30

If you want to enable dark night mode globally within the app, you need to meet the following conditions:

  1. The corresponding Activity should inherit from AppCompatActivity.

  2. Call the following code at the appropriate place to enable global dark mode in the application.

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
    

For API 31 and above

If you want to enable dark night mode globally within the app, you need to meet the following conditions:

  1. Get UiModeManager .

    private val uiManager: UiModeManager by lazy {
         getSystemService(UI_MODE_SERVICE) as UiModeManager
    }
    
  2. Call the following code at the appropriate place to enable global dark mode in the application.

    uiManager.setApplicationNightMode(UiModeManager.MODE_NIGHT_YES)
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文