android中屏幕亮度相关设置

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

测试Activity:

package com.home.screenbrightness;

import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class TestScreenBrightnessActivity extends Activity {
	private int brightness;
	private SeekBar seekbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		seekbar = (SeekBar) findViewById(R.id.main_sb);
		initBrightness();
		seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				brightness = seekBar.getProgress();
				seekBar.setProgress(brightness);
				SetAndGetDataUtil.SetData(getApplicationContext(), "test",
						"light", brightness + "");
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				int curProgress = seekBar.getProgress();// 得到当前进度值
				// 当进度小于70时,设置成70,防止太黑看不见的情况。
				if (curProgress < 70) {
					curProgress = 70;
				}
				// 根据当前进度改变屏幕亮度
				Settings.System.putInt(getContentResolver(),
						Settings.System.SCREEN_BRIGHTNESS, curProgress);
				curProgress = Settings.System.getInt(getContentResolver(),
						Settings.System.SCREEN_BRIGHTNESS, -1);
				BrightnessUtil.setBrightness(TestScreenBrightnessActivity.this,
						curProgress);
				// BrightnessUtil.saveBrightness(TestScreenBrightnessActivity.this,
				// curProgress);
			}
		});
	}

	/**
	 * 初始化屏幕亮度值
	 */
	private void initBrightness() {
		// 如果开启了自动亮度调节,则关闭之
		if (BrightnessUtil.isAutoBrightness(this)) {
			BrightnessUtil.stopAutoBrightness(this);
		}
		// 读取文件中设置的亮度值
		String light = SetAndGetDataUtil.GetData(this, "test", "light");
		if (!"".equals(light)) {
			brightness = Integer.valueOf(light);
		} else {
			brightness = BrightnessUtil.getScreenBrightness(this);
		}
		BrightnessUtil.setBrightness(this, brightness);
		seekbar.setProgress(brightness);
	}
}

存取数据工具类:SetAndGetDataUtil

package com.home.screenbrightness;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 保存数据的工具类
 * 
 * @author Administrator
 * 
 */
public class SetAndGetDataUtil {
	private static SharedPreferences sp;

	@SuppressWarnings("static-access")
	public static void SetData(Context context, String filename, String key,
			String value) {
		sp = context.getSharedPreferences(filename, context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString(key, value);
		editor.commit();
	}

	@SuppressWarnings("static-access")
	public static String GetData(Context context, String filename, String key) {
		String value = "";
		sp = context.getSharedPreferences(filename, context.MODE_PRIVATE);
		value = sp.getString(key, "");
		return value;
	}
}

屏幕亮度处理工具类:

package com.home.screenbrightness;

import android.app.Activity;
import android.content.ContentResolver;
import android.net.Uri;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.WindowManager;

/**
 * 处理屏幕亮度的工具类
 * 
 * @author Administrator
 * 
 */
public class BrightnessUtil {
	/**
	 * 判断是否开启了自动亮度调节
	 * 
	 * @param activity
	 * @return
	 */
	public static boolean isAutoBrightness(Activity activity) {
		boolean isAutoAdjustBright = false;
		try {
			isAutoAdjustBright = Settings.System.getInt(
					activity.getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
		} catch (SettingNotFoundException e) {
			e.printStackTrace();
		}
		return isAutoAdjustBright;
	}

	/**
	 * 获取屏幕的亮度
	 * 
	 * @param activity
	 * @return
	 */
	public static int getScreenBrightness(Activity activity) {
		int brightnessValue = 0;
		try {
			brightnessValue = android.provider.Settings.System.getInt(
					activity.getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return brightnessValue;
	}

	/**
	 * 设置屏幕亮度
	 * 
	 * @param activity
	 * @param brightness
	 */
	public static void setBrightness(Activity activity, int brightness) {
		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
		activity.getWindow().setAttributes(lp);
	}

	/**
	 * 关闭亮度自动调节
	 * 
	 * @param activity
	 */
	public static void stopAutoBrightness(Activity activity) {
		Settings.System.putInt(activity.getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
	}

	/**
	 * 开启亮度自动调节
	 * 
	 * @param activity
	 */

	public static void startAutoBrightness(Activity activity) {
		Settings.System.putInt(activity.getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
	}

	/**
	 * 保存亮度设置状态
	 * 
	 * @param activity
	 * @param brightness
	 */
	public static void saveBrightness(Activity activity, int brightness) {
		Uri uri = android.provider.Settings.System
				.getUriFor("screen_brightness");
		ContentResolver resolver = activity.getContentResolver();
		android.provider.Settings.System.putInt(resolver, "screen_brightness",
				brightness);
		resolver.notifyChange(uri, null);
	}

}

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <SeekBar
        android:id="@+id/main_sb"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255" />

</LinearLayout>


记得加上权限:

 <uses-permission android:name="android.permission.WRITE_SETTINGS" />





 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值