即使在杀死应用程序后,如何保持黑暗模式?
这是我在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经给出了有关此但是我也在这里提供:
创建一个名为preverences Manager
的类
粘贴此代码的类
粘贴此代码
I have already given an answer about this here but I am giving it here also:
Create a class named Preferences Manager
Paste this code in the class
????
Then in the fragment make an object named preferences manager like this.
PreferencesManager preferencesManager = new PreferencesManager(getActivity());
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
Edit
Alternatively you can also use my library here to store the data. To store and retrieve, see the
README.md
file该答案提供了Android的适应深夜模式。深色主题适用于Android 10(API级别29)且更高。这是一个先决条件。
对于API 29 -API 30 ,
如果要在应用程序中全球启用Dark Night模式,则需要满足以下条件:
相应的活动应从AppCompatactivity继承。
在适当的位置调用以下代码,以在应用程序中启用全局暗模式。
对于API 31及以上的API ,
如果您想在应用程序中全球启用深夜模式,则需要满足以下条件:
获取UimodeManager。
在
GetSystemService(UI_Mode_Service)作为UimodeManager
}
在适当的位置调用以下代码,以在应用程序中启用全局暗模式。
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:
The corresponding Activity should inherit from AppCompatActivity.
Call the following code at the appropriate place to enable global dark mode in the application.
For API 31 and above
If you want to enable dark night mode globally within the app, you need to meet the following conditions:
Get UiModeManager .
Call the following code at the appropriate place to enable global dark mode in the application.