安卓修改屏幕亮度

效果:启动Activity,按加减图标,按下加减时图标变暗,增加减少屏幕亮度值

1.Activity

public class BrightnessSettingActivity extends BaseActivity {
    private Button btadd,btdrop;
    private ImageView brightnessImageValue;
    int currentBrightnessValue;
    ContentResolver resolver;
    int maxValue;
    int scale;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            if (!Settings.System.canWrite(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                intent.setData(Uri.parse("package:" + this.getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                this.startActivity(intent);
            } else {
                initView();
            }
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void initView() throws Settings.SettingNotFoundException {
        setContentView(R.layout.activity_brightness_setting);
        btadd=(Button) findViewById(R.id.add_brightness);
        btdrop=(Button)findViewById(R.id.drop_brightness);
        brightnessImageValue=(ImageView)findViewById(R.id.brightness_value);
        currentBrightnessValue = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        try {
            Class<?> powerManagerClass = Class.forName("android.os.PowerManager");
            Field field = powerManagerClass.getDeclaredField("BRIGHTNESS_ON");
            maxValue = (int) field.get(null);
            scale = maxValue/4;
        } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
            // 处理异常
        }
        Log.e("xwd","current value:"+currentBrightnessValue);
        Log.e("xwd","max value:"+maxValue);
        Log.e("xwd","scale value:"+scale);
        // 获取 ContentResolver
        resolver = getContentResolver();

        btadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    currentBrightnessValue = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
                    if ((currentBrightnessValue+scale)<maxValue) {
                    currentBrightnessValue+=scale;
                    }
                    else
                    {
                        currentBrightnessValue=maxValue;
                    }
                    setBrightnessValue(currentBrightnessValue);
                } catch (Settings.SettingNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        btdrop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    currentBrightnessValue = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
                    if ((currentBrightnessValue-scale)>0) {
                        currentBrightnessValue -= scale;
                    }
                    else
                    {
                        currentBrightnessValue = 0;
                    }
                    setBrightnessValue(currentBrightnessValue);
                } catch (Settings.SettingNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void setBrightnessValue(int value)
    {
        // 修改系统亮度设置(需要 WRITE_SETTINGS 权限)
        Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, value);

    // 应用修改后的设置
        Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }

}

2.布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/titleHeight"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/brightness_titleview"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/titleHeight"
            android:gravity="center|center_horizontal"
            android:layout_weight="1"
            android:paddingLeft="@dimen/listpadding"
            android:text="@string/brightness"
            android:textColor="@color/white"
            android:textSize="@dimen/title_font_size" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/brightness_value"
            android:src="@drawable/brightness"
            >
        </ImageView>

    </LinearLayout>
    <RelativeLayout
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/drop_brightness"
            android:background="@drawable/button_drop"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="100dp"
            >
        </Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/add_brightness"
            android:background="@drawable/button_add"
            android:layout_alignParentRight="true"
            android:layout_marginRight="100dp"
            >
        </Button>
    </RelativeLayout>

</LinearLayout>

3.button增减图标按下效果添加

//button_add.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <bitmap android:src="@drawable/volume_add_down" />
    </item>
    <item>
        <bitmap android:src="@drawable/volume_add" />
    </item>
</selector>


//button_drop.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <bitmap android:src="@drawable/volume_drop_down" />
    </item>
    <item>
        <bitmap android:src="@drawable/volume_drop" />
    </item>
</selector>

4.运行效果图

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值