下面列出了android.support.v7.app.AppCompatDelegate#setDefaultNightMode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.night_mode) {
// Get the night mode state of the app.
int nightMode = AppCompatDelegate.getDefaultNightMode();
//Set the theme mode for the restarted activity
if (nightMode == AppCompatDelegate.MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode
(AppCompatDelegate.MODE_NIGHT_NO);
} else {
AppCompatDelegate.setDefaultNightMode
(AppCompatDelegate.MODE_NIGHT_YES);
}
// Recreate the activity for the theme change to take effect.
recreate();
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// Don't initialize analytics with debug build
if (!BuildConfig.DEBUG) {
Fabric.with(this, new Crashlytics());
}
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
Utils.DARK_THEME = Themes.isDarkTheme(this);
Themes.Theme theme = Themes.getTheme(this, Utils.DARK_THEME);
if (Utils.DARK_THEME) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
setTheme(theme.getStyle());
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && setStatusBarColor()) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(statusBarColor());
}
}
private void initDayNightMode() {
if (SharedPreferencesUtil.isNightMode()) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
@Override
public void onCreate() {
super.onCreate();
_context = getApplicationContext();
instance = this;
PreferencesUtils preferencesUtils = new PreferencesUtils(this);
if(preferencesUtils.getBoolean(R.string.action_day_night, false)){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}else {
//设置该app的主题根据时间不同显示
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
}
}
private void initTheme() {
//设置该app的主题根据时间不同显示
if (AppConfig.isNightMode()) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.night_mode){
int nightMode = AppCompatDelegate.getDefaultNightMode();
if (nightMode == AppCompatDelegate.MODE_NIGHT_YES){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
recreate();
}
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String key = preference.getKey();
if (key.equals(KEY_THEME)) {
if (value.equals(QuickNote.getString(R.string.theme_value_default))) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else if (value.equals(QuickNote.getString(R.string.theme_value_night))) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
getActivity().recreate();
} else if (key.equals(KEY_LANG)) {
if (value.equals(QuickNote.getString(R.string.lang_value_chinese))) {
CommonUtil.changeLocalLanguage(QuickNote.getAppContext(), Locale.SIMPLIFIED_CHINESE);
} else if (value.equals(QuickNote.getString(R.string.lang_value_english))) {
CommonUtil.changeLocalLanguage(QuickNote.getAppContext(), Locale.US);
}
getActivity().recreate();
} else if (key.equals(KEY_AUTO_UPDATE)) {
if (value.equals(false)) { // 提示用户如需更新可到关于界面进行手动更新
CommonUtil.showSnackBarOnUiThread(getView(), R.string.prompt_close_auto_update);
}
} else if (key.equals(KEY_GESTURE_PWD)) {
if (value.equals(true)) {
if (TextUtils.isEmpty(QuickNote.getSavedPattern())) { // 如果是初次启用
Intent intent = new Intent(getActivity(), LockActivity.class);
startActivity(intent);
}
}
}
return true;
}
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
Realm.init(this);
if (PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(SettingsUtil.KEY_NIGHT_MODE, false)) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
public void applyNightModel(AppCompatActivity activity){
invokeResources(activity);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
activity.getDelegate().applyDayNight();
applyNewModel();
PersistenceUtils.setNightModel(activity.getApplicationContext(), true);
ModelChangeManager.getInstance().notifyChange(true);
}
/**
* Returns true if the current UI_MODE_NIGHT is enabled, false otherwise.
*
* @param context to retrieve the current configuration
*/
private static boolean isNightModeEnabled(Context context) {
if (isNightModeFollowSystem()) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
}
int uiMode = retrieveCurrentUiMode(context);
return uiMode == Configuration.UI_MODE_NIGHT_YES;
}
/**
* 设置是否使用夜间模式,这里只是切换主题设置,具体的主题样式资源等还需要自己配置,
* 参考项目的 values-night 配置
*
* @param night 是否设置为夜间模式
*/
public static void setNightTheme(boolean night) {
if (night) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
@Override
public void useNightMode(boolean isNight) {
if (isNight) {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_NO);
}
recreate();
}
public static void setNightTheme(AppCompatActivity activity) {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
activity.getDelegate().setLocalNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
// 调用 recreate() 使设置生效
activity.recreate();
}
protected void initNightMode() {
boolean isNight = MyMusicUtil.getNightMode(context);
if (isNight) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.night_mode){
int nightMode = AppCompatDelegate.getDefaultNightMode();
if (nightMode == AppCompatDelegate.MODE_NIGHT_YES){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
recreate();
}
return true;
}
private void continueInit() {
if (this.binder == null) {
return;
}
this.startState += 1;
switch (this.startState) {
case 1:
log("init 1: load database");
// open without password
this.binder.loadDatabase();
continueInit();
break;
case 2:
log("init 2: check database");
if (this.binder.getDatabase() == null) {
// database is probably encrypted
showDatabasePasswordDialog();
} else {
continueInit();
}
break;
case 3:
log("init 3: check username");
if (this.binder.getSettings().getUsername().isEmpty()) {
// set username
showMissingUsernameDialog();
} else {
continueInit();
}
break;
case 4:
log("init 4: check key pair");
if (this.binder.getSettings().getPublicKey() == null) {
// generate key pair
initKeyPair();
}
continueInit();
break;
case 5:
log("init 5: check addresses");
if (this.binder.isFirstStart()) {
showMissingAddressDialog();
} else {
continueInit();
}
break;
case 6:
log("init 6: battery optimizations");
if (this.binder.isFirstStart() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
PowerManager pMgr = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
if (!pMgr.isIgnoringBatteryOptimizations(this.getPackageName())) {
Intent intent = new Intent(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + this.getPackageName()));
startActivityForResult(intent, IGNORE_BATTERY_OPTIMIZATION_REQUEST);
break;
}
} catch(Exception e) {
// ignore
}
}
continueInit();
break;
case 7:
log("init 7: start contact list");
// set night mode
boolean nightMode = this.binder.getSettings().getNightMode();
AppCompatDelegate.setDefaultNightMode(
nightMode ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO
);
// all done - show contact list
startActivity(new Intent(this, MainActivity.class));
finish();
break;
}
}
private void initNightMode() {
boolean isNightMode = dataManager.isOpenNightMode();
AppCompatDelegate.setDefaultNightMode(isNightMode ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
}
private void refreshNightMode(int value){
result_code=RESULT_OK;
AppCompatDelegate.setDefaultNightMode(value);
recreate();
}
public static void changeThemeStyle(AppCompatDelegate delegate) {
int nightMode = Integer.valueOf(SettingsActivity.getThemeStyle());
AppCompatDelegate.setDefaultNightMode(nightMode);
delegate.setLocalNightMode(nightMode);
}
private static void setNight(){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
SharedPreferences.Editor editor = getSp().edit();
editor.putString(MODE,NIGHT);
editor.apply();
}