import android.app.Activity;import android.content.SharedPreferences;import android.content.res.Configuration;import android.preference.PreferenceManager;import java.lang.ref.WeakReference;/** * Night Mode Helper * 

* Helps use utilise the night and notnight resource qualifiers without being in car or dock mode. *

* Implementation is simple. Add the follow line at the top of your activity's onCreate just after * the super.onCreate(); The idea here is to do it before we create any views. So the new views will * use the correct Configuration. * *

 * mNightModeHelper = new NightModeHelper(this, R.style.AppTheme); * 
* * You can now use your instance of NightModeHelper to control which mode you are in. You can choose * to persist the current setting and hand it back to this class as the defaultUiMode, otherwise * this is done for you automatically. *

* I'd suggest you setup your Theme as follows: *

    *
  • res\values\styles.xml * *
     * <style name="AppTheme" parent="AppBaseTheme"></style> * 
    * *
  • *
  • res\values-night\styles.xml * *
     * <style name="AppBaseTheme" parent="@android:style/Theme.Holo"></style> * 
    * *
  • *
  • res\values-notnight\styles.xml * *
     * <style name="AppBaseTheme" parent="@android:style/Theme.Holo.Light"></style> * 
    * *
  • *
* * @author Simon Lightfoot */
public class NightModeHelper { private static final String PREF_KEY = "nightModeState"; private static int sUiNightMode = Configuration.UI_MODE_NIGHT_UNDEFINED; private WeakReference mActivity; private SharedPreferences mPrefs; /** * Default behaviour is to automatically save the setting and restore it. */ public NightModeHelper(Activity activity, int theme) { int currentMode = (activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK); mPrefs = PreferenceManager.getDefaultSharedPreferences(activity); init(activity, theme, mPrefs.getInt(PREF_KEY, currentMode)); } /** * If you don't want the autoSave feature and instead want to provide your own persisted storage * for the mode, use the defaultUiMode for it. */ public NightModeHelper(Activity activity, int theme, int defaultUiMode) { init(activity, theme, defaultUiMode); } public static int getUiNightMode() { return sUiNightMode; } private void init(Activity activity, int theme, int defaultUiMode) { mActivity = new WeakReference(activity); if (sUiNightMode == Configuration.UI_MODE_NIGHT_UNDEFINED) { sUiNightMode = defaultUiMode; } updateConfig(sUiNightMode); // This may seem pointless but it forces the Theme to be reloaded // with new styles that would change due to new Configuration. activity.setTheme(theme); } private void updateConfig(int uiNightMode) { Activity activity = mActivity.get(); if (activity == null) { throw new IllegalStateException("Activity went away?"); } Configuration newConfig = new Configuration(activity.getResources().getConfiguration()); newConfig.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK; newConfig.uiMode |= uiNightMode; activity.getResources().updateConfiguration(newConfig, null); sUiNightMode = uiNightMode; if (mPrefs != null) { mPrefs.edit() .putInt(PREF_KEY, sUiNightMode) .apply(); } } public void toggle() { if (sUiNightMode == Configuration.UI_MODE_NIGHT_YES) { notNight(); } else { night(); } } public void notNight() { updateConfig(Configuration.UI_MODE_NIGHT_NO); mActivity.get().recreate(); } public void night() { updateConfig(Configuration.UI_MODE_NIGHT_YES); mActivity.get().recreate(); }}

转自https://gist.github.com/slightfoot/c508cdc8828a478572e0

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 关于 android Intent 传对象和对象数组的
  2. cocos2dx - 部署到android真机上错误整理
  3. 关于android全屏截图,无需root,无状态栏,2
  4. (持续更新, 目前含100+工具类) DevUtils
  5. Android(安卓)WebView属性及用法
  6. Android使用intent调取导航或者地图
  7. Android(安卓)Fragment数据传递
  8. Android:混合轮播视频和图片
  9. Gradle Build加速
  10. Android(安卓)SeekBar