<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
这个权限一定要加上,不然会出现进入设置界面,但该权限选项不能点击的效果
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.SeekBar;
import com.example.zl.myapplication.R;
public class LightActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
SeekBar seekbar_light;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_light);
//设置App亮度和系统亮度一致
changeAppBrightness(getSystemBrightness());
/**
* 亮度调节
*/
seekbar_light = findViewById(R.id.seekbar_light);
//因为亮度范围是 0 - 255
seekbar_light.setMax(255);
//设置进度条是系统亮度的长度
seekbar_light.setProgress(getSystemBrightness());
seekbar_light.setOnSeekBarChangeListener(this);
}
/**
* 获得系统亮度
*
* @return
*/
private int getSystemBrightness() {
ContentResolver contentResolver = getContentResolver();
int defVal = 125;
return Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, defVal);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// 设置系统亮度
changeAppBrightness(progress);
}
/**
* Seekbar开始滚动的回调函数
*
* @param seekBar
*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
/**
* Seekbar结束滚动的回调函数
*
* @param seekBar
*/
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
/**
* 当屏幕亮度模式为0即手动调节时,可以通过如下代码设置屏幕亮度:
*
* @param brightness
*/
public void changeAppBrightness(int brightness) {
//申请android.permission.WRITE_SETTINGS权限的方式
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//如果当前平台版本大于23平台
if (!Settings.System.canWrite(this)) {
//如果没有修改系统的权限这请求修改系统的权限
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
//有了权限,你要做什么呢?具体的动作
setScrennManualMode();
ContentResolver contentResolver = getContentResolver();
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
}
}
}
/**
* 如果需要实现亮度调节,首先需要设置屏幕亮度调节模式为手动模式。
* Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC:值为1,自动调节亮度。
* Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL:值为0,手动模式。
*/
public void setScrennManualMode() {
ContentResolver contentResolver = getContentResolver();
try {
int mode = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE);
if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}
}
R.layout.activity_light
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/liangdutiaoshi_an" />
<SeekBar
android:id="@+id/seekbar_light"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="true"
android:max="100"
android:progress="10"
android:maxHeight="4dp"
android:minHeight="4dp"
android:progressDrawable="@drawable/po_seekbar"
android:thumb="@drawable/seekbar_thumb" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/liangdutiaoshi_liang" />
</LinearLayout>
liangdutiaoshi_an.png
liangdutiaoshi_liang.png
po_seekbar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid android:color="#5000" />
<corners android:radius="10dp"/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="#5000" />
<corners android:radius="10dp"/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#417ea5" />
<corners android:radius="2dp"/>
</shape>
</clip>
</item>
</layer-list>
seekbar_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/dian_blue"
android:state_focused="true"
android:state_pressed="false" />
<item
android:drawable="@drawable/dian_blue"
android:state_focused="true"
android:state_pressed="true" />
<item
android:drawable="@drawable/dian_blue"
android:state_focused="false"
android:state_pressed="true" />
<item
android:drawable="@drawable/dian_blue" />
</selector>
dain_blue.png