Android 调节当前Activity的屏幕亮度
调节的关键代码:
WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.screenBrightness = Float.parseFloat(brightNum); getWindow().setAttributes(layoutParams);
以下是一个例子,包含拖动条控件:
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入亮度(0-1)" /> <Button android:id="@+id/btn_set" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设置一下当前亮度" android:onClick="changeBright" /> <Button android:id="@+id/btn_set" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设置一下当前亮度 [0, 1]" android:onClick="changeBright2" /> <Button android:id="@+id/btn_current" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="获取当前亮度(非自动调节模式生效)" android:onClick="showCurrentBright" /> <SeekBar android:id="@+id/seek_bar" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设置为自动调节屏幕亮度" android:onClick="setAutoBright" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设置为手动调节屏幕亮度" android:onClick="setManualBright" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取亮度的模式(与系统设置相关)" android:onClick="showScreenMode" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="让手机直接黑屏..." android:onClick="setNoBright" /> </LinearLayout>
Activity:
package com.example.screenBrightnessTest; import android.app.Activity; import android.content.ContentResolver; import android.os.Bundle; import android.provider.Settings; import android.view.View; import android.view.WindowManager; import android.widget.EditText; import android.widget.SeekBar; import android.widget.Toast; public class MyActivity extends Activity { private EditText editText; private SeekBar seekBar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.et); seekBar = (SeekBar) findViewById(R.id.seek_bar); //设置滚动条 seekBar.setProgress(getCurrentBrightValue()); seekBar.setMax(255); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { /** * 拖动中数值的时候 * @param fromUser 是否是由用户操作的 */ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (progress > 3 && fromUser) {//以免太暗 WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.screenBrightness = (float) progress / 255;//因为这个值是[0, 1]范围的 getWindow().setAttributes(layoutParams); } } /** * 当按下的时候 */ @Override public void onStartTrackingTouch(SeekBar seekBar) { System.out.println("com.example.screenBrightnessTest.MyActivity.onStartTrackingTouch"); } /** *当松开的时候 */ @Override public void onStopTrackingTouch(SeekBar seekBar) { System.out.println("com.example.screenBrightnessTest.MyActivity.onStopTrackingTouch"); } }); } public void changeBright(View view) { String brightNum = editText.getText().toString(); WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.screenBrightness = Float.parseFloat(brightNum); getWindow().setAttributes(layoutParams); } /** * 仅当系统的亮度模式是非自动模式的情况下,获取当前屏幕亮度值[0, 255]. * 如果是自动模式,那么该方法获得的值不正确。 */ private int getCurrentBrightValue() { int nowBrightnessValue = 0; ContentResolver resolver = getContentResolver(); try { nowBrightnessValue = android.provider.Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS, 255); } catch (Exception e) { e.printStackTrace(); } return nowBrightnessValue; } /** * 获取当前的亮度 */ private float getMaxBrightValue2() { return getWindow().getAttributes().screenBrightness; } public void showCurrentBright(View view) { Toast.makeText(this, "当前系统亮度的值为:" + getCurrentBrightValue(), 1).show(); } /** * 让手机直接黑屏 */ public void setNoBright(View view) { getWindow().getAttributes().screenBrightness = 0; } /** * 设置当前屏幕亮度的模式 * * @param paramInt Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC或者Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL */ private void setScreenMode(int paramInt) { try { Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, paramInt); } catch (Exception localException) { localException.printStackTrace(); } } /** * 设置为自动调节亮度 * 如果当前系统的模式不是自动调节模式,那么才会生效 */ public void setAutoBright(View view) { setScreenMode(Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); } /** * 获得当前屏幕亮度的模式 */ public void showScreenMode(View view) { int screenMode = 0; try { screenMode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE); switch (screenMode) { case Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC: Toast.makeText(this, "自动亮度模式", 1).show(); break; default: Toast.makeText(this, "手动调节亮度", 1).show(); } } catch (Exception e) { e.printStackTrace(); } } public void setManualBright(View view) { setScreenMode(Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); } public void changeBright2(View view) { Toast.makeText(this, "亮度值[0, 1] = " + getWindow().getAttributes().screenBrightness, 1).show(); } }
本文出自 无忧之路 - 博客园