|
| 1 | +package com.clock.study.activity; |
| 2 | + |
| 3 | +import android.app.UiModeManager; |
| 4 | +import android.content.Context; |
| 5 | +import android.support.v7.app.AppCompatActivity; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.util.Log; |
| 8 | +import android.widget.RadioButton; |
| 9 | +import android.widget.RadioGroup; |
| 10 | + |
| 11 | +import com.clock.study.R; |
| 12 | + |
| 13 | +/** |
| 14 | + * 夜间模式实现方案 |
| 15 | + * |
| 16 | + * @author Clock |
| 17 | + * @since 2016=08-11 |
| 18 | + */ |
| 19 | +public class NightModeActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { |
| 20 | + |
| 21 | + private final static String TAG = NightModeActivity.class.getSimpleName(); |
| 22 | + |
| 23 | + private UiModeManager mUiModeManager; |
| 24 | + private RadioGroup mUiModeGroup; |
| 25 | + |
| 26 | + @Override |
| 27 | + protected void onCreate(Bundle savedInstanceState) { |
| 28 | + super.onCreate(savedInstanceState); |
| 29 | + setContentView(R.layout.activity_night_mode); |
| 30 | + |
| 31 | + mUiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE); |
| 32 | + |
| 33 | + mUiModeGroup = (RadioGroup) findViewById(R.id.rg_ui_mode); |
| 34 | + mUiModeGroup.setOnCheckedChangeListener(this); |
| 35 | + RadioButton normalMode = (RadioButton) findViewById(R.id.rb_normal); |
| 36 | + normalMode.setChecked(true); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void onCheckedChanged(RadioGroup group, int checkedId) { |
| 42 | + if (mUiModeGroup == group) { |
| 43 | + int currentModeType = mUiModeManager.getCurrentModeType(); |
| 44 | + Log.i(TAG, "currentModeType: " + currentModeType); |
| 45 | + if (checkedId == R.id.rb_normal) { |
| 46 | + mUiModeManager.disableCarMode(0); |
| 47 | + mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO); |
| 48 | + |
| 49 | + } else if (checkedId == R.id.rb_night_mode) {//夜间模式 |
| 50 | + mUiModeManager.enableCarMode(0); |
| 51 | + mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_YES); |
| 52 | + |
| 53 | + } else if (checkedId == R.id.rb_car_mode) {//车载模式 |
| 54 | + mUiModeManager.disableCarMode(0); |
| 55 | + mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO); |
| 56 | + |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments