第一次执行夜间模式代码时,它会重新启动 Activity
第二次,代码将正常工作并对程序进行更改
我使用了以下代码:
public class Splash extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new CheckedNightMode().execute();
}
private class CheckedNightMode extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
SharedPreferences sharedpreferences = getApplicationContext().getSharedPreferences("Night_Mode", Context.MODE_PRIVATE);
boolean state = sharedpreferences.getBoolean("State", false);
if (state) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
checkLanguage();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(Splash.this, Main.class));
finish();
}
}, 3000);
}
private void checkLanguage() {
SharedPreferences sharedpreferences = getApplicationContext().getSharedPreferences("Language", Context.MODE_PRIVATE);
String checkedLanguage = sharedpreferences.getString("Select", "en");
setLanguageState(checkedLanguage);
}
主题Theme.MaterialComponents.DayNight.NoActionBar我试过了,但还是一样
交互式爱情
达令说
相关分类