工具代码
public class SystemBrightManager {
// 判断是否开启了自动亮度调节
public static boolean isAutoBrightness(Activity activity) {
boolean autoBrightness = false;
ContentResolver contentResolver = activity.getContentResolver();
try {
autoBrightness = Settings.System.getInt(contentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
return autoBrightness;
}
// 获取当前系统亮度值
public static int getBrightness(Activity activity) {
int brightValue = 0;
ContentResolver contentResolver = activity.getContentResolver();
try {
brightValue = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
return brightValue;
}
// 改变屏幕亮度
public static void setBrightness(Activity activity, int brightValue) {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.screenBrightness = (brightValue <= 0 ? -1.0f : brightValue / 255f);
activity.getWindow().setAttributes(lp);
}
// 开启亮度自动亮度模式
public static void startAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
Uri uri = Settings.System.getUriFor("screen_brightness");
activity.getContentResolver().notifyChange(uri, null);
}
// 停止自动亮度模式
public static void stopAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
Uri uri = Settings.System.getUriFor("screen_brightness");
activity.getContentResolver().notifyChange(uri, null);
}
/**
* 设置当前屏幕亮度的模式
* SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度
* SCREEN_BRIGHTNESS_MODE_MANUAL=0 为手动调节屏幕亮度
*/
public static void setBrightnessMode(Activity activity, int brightMode) {
Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, brightMode);
}
}
xml类代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/app_name"
android:textColor="@color/black"
android:textSize="16sp" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/change"
android:textColor="@color/black"
android:textSize="13sp" />
<SeekBar
android:id="@+id/light_seekBar"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:maxHeight="10.0dip"
android:minHeight="4.0dip"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:progress="75"
android:max="255" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:onClick="System"
android:text="@string/star"
android:background="@color/white"
android:textColor="@color/black" />
</RelativeLayout>
values/colors.xml && strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
<color name="black">#000000</color> <!-- 黑色 -->
<color name="white">#ffffff</color> <!-- 白色 -->
<color name="red">#ff0000</color> <!-- 红色 -->
</resources>
<resources>
<string name="app_name">My Application</string>
<string name="hello">测试系统亮度</string>
<string name="change">调节亮度</string>
<string name="star">启用系统亮度</string>
<string name="close">关闭系统亮度</string>
</resources>
Activity
package com.wd.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
private Button button;
private SeekBar lightSeek;
private boolean flag = false;
private int defalutValue = 75;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
lightSeek = (SeekBar) findViewById(R.id.light_seekBar);
lightSeek.setOnSeekBarChangeListener(seekBarChange);
}
public void System (View v) {
if (flag) {
flag = false;
button.setTextColor(getResources().getColor(R.color.black));
button.setText(getResources().getString(R.string.star));
setScreenLight(defalutValue);
} else {
flag = true;
button.setTextColor(getResources().getColor(R.color.red));
button.setText(getResources().getString(R.string.close));
boolean isAutoBrightness = SystemBrightManager.isAutoBrightness(this);
if (isAutoBrightness) { // �Զ���������
SystemBrightManager.setBrightness(this, -1);
} else {
int brightValue = SystemBrightManager.getBrightness(this);
SystemBrightManager.setBrightness(this, brightValue);
}
}
}
private SeekBar.OnSeekBarChangeListener seekBarChange = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setScreenLight(progress);
}
};
public void setScreenLight(int progress) {
if (progress < 1) {
progress = 1;
} else if (progress > 255) {
progress = 255;
}
final WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.screenBrightness = progress / 255f;
getWindow().setAttributes(attrs);
defalutValue = progress;
}
}