这两天遇到一个需求,通过SeekBar滑动改变系统的亮度,网上找了好几拨资料,才写好。所以决定把这部分代码贴出来,给大家参考下。
把大象塞进冰箱需要几步,对,三步
1、第一步,清单文件中加入权限
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
在6.0以下可以直接用,6.0以上需要权限申请,这里暂不赘述
2、获取系统当前亮度方法
该方法是为了便利检索单个系统设置值,且值为整数。请注意,内部设置值总是存储为字符串;此函数默认将字符串转换为整数。如果没有该设置或是字符串值不是一个数字,抛出SettingNotFoundException异常
try {
int currentBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
}
在android中,许多的系统属性都在settings应用当中进行设置的,比如wifi、蓝牙状态,当前本机语言,屏幕亮度等等一些相关的系统属性值。这些数据主要是存储在数据库中。
3、修改系统当前亮度方法
该方法便于更新一个单一的整数设置值,请注意,内部设置值总是存储为字符串,因此此函数将给定值转换为字符串存储之前。
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 15);//修改当前亮度为15,系统亮度范围为0-255
下面是完整代码示例
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_system_light"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.dao_li.myapplication.socketTest.SystemLightActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水平滑动调节亮度"
android:textSize="20sp" />
<SeekBar
android:id="@+id/seekbar_light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:max="255" />
<TextView
android:id="@+id/tv_light_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
Activity代码
package com.dao_li.myapplication.socketTest;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.widget.SeekBar;
import android.widget.TextView;
import com.dao_li.myapplication.R;
import butterknife.Bind;
import butterknife.ButterKnife;
public class SystemLightActivity extends AppCompatActivity {
@Bind(R.id.seekbar_light)
SeekBar seekbarLight;
@Bind(R.id.tv_light_value)
TextView tvLightValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_system_light);
ButterKnife.bind(this);
initView();
initSystemLight();
}
private void initView() {
// 调节亮度
seekbarLight.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
tvLightValue.setText("亮度值:" + progress);
Uri uri = Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, progress);
getContentResolver().notifyChange(uri, null);
}
});
}
private void initSystemLight() {
int currentBrightness = 0;
try {
currentBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
}
seekbarLight.setProgress(currentBrightness);
tvLightValue.setText("亮度值:" + currentBrightness + "");
}
}
好,大象已经完整的塞进冰箱了。