Skip to content

Commit 06f36c4

Browse files
committed
添加UiModeManager使用
1 parent 69bc41e commit 06f36c4

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<activity
3939
android:name=".activity.AnimatorActivity"
4040
android:label="@string/android_animator" />
41+
<activity android:name=".activity.NightModeActivity"></activity>
4142
</application>
4243

4344
</manifest>

src/main/java/com/clock/study/activity/MainActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ protected void onCreate(Bundle savedInstanceState) {
1717
findViewById(R.id.btn_camera_take_photo).setOnClickListener(this);
1818
findViewById(R.id.btn_animation).setOnClickListener(this);
1919
findViewById(R.id.btn_animator).setOnClickListener(this);
20+
findViewById(R.id.btn_night_mode).setOnClickListener(this);
2021

2122
}
2223

@@ -32,6 +33,9 @@ public void onClick(View v) {
3233
} else if (viewId == R.id.btn_animator) {
3334
Intent animatorIntent = new Intent(this, AnimatorActivity.class);
3435
startActivity(animatorIntent);
36+
} else if (viewId == R.id.btn_night_mode) {
37+
Intent animatorIntent = new Intent(this, NightModeActivity.class);
38+
startActivity(animatorIntent);
3539
}
3640
}
3741
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

src/main/res/layout/activity_main.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,11 @@
2525
android:layout_width="match_parent"
2626
android:layout_height="wrap_content"
2727
android:text="Android Animator" />
28+
29+
<Button
30+
android:id="@+id/btn_night_mode"
31+
android:layout_width="match_parent"
32+
android:layout_height="wrap_content"
33+
android:text="夜间模式" />
34+
2835
</LinearLayout>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical"
7+
android:paddingBottom="@dimen/activity_vertical_margin"
8+
android:paddingLeft="@dimen/activity_horizontal_margin"
9+
android:paddingRight="@dimen/activity_horizontal_margin"
10+
android:paddingTop="@dimen/activity_vertical_margin"
11+
tools:context=".activity.NightModeActivity">
12+
13+
<TextView
14+
android:layout_width="wrap_content"
15+
android:layout_height="wrap_content"
16+
android:text="UiModeManager:" />
17+
18+
<RadioGroup
19+
android:id="@+id/rg_ui_mode"
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"
22+
android:orientation="horizontal">
23+
24+
<RadioButton
25+
android:id="@+id/rb_normal"
26+
android:layout_width="0dp"
27+
android:layout_height="wrap_content"
28+
android:layout_weight="1"
29+
android:text="Normal" />
30+
31+
<RadioButton
32+
android:id="@+id/rb_car_mode"
33+
android:layout_width="0dp"
34+
android:layout_height="wrap_content"
35+
android:layout_weight="1"
36+
android:text="Car Mode" />
37+
38+
<RadioButton
39+
android:id="@+id/rb_night_mode"
40+
android:layout_width="0dp"
41+
android:layout_height="wrap_content"
42+
android:layout_weight="1"
43+
android:text="Night Mode" />
44+
</RadioGroup>
45+
</LinearLayout>

0 commit comments

Comments
 (0)