关闭

获取和设置系统锁屏,屏幕亮度

591人阅读 评论(0) 收藏 举报
分类:

一些应用中可用调节屏幕的亮,和锁屏时间.我们可通过调用系统的接口改变其中的值来获得我们想要的结果.

锁屏代码如下:

获取系统锁屏的时间
screenOffTime = Settings.System.getInt(getContentResolver(),
					Settings.System.SCREEN_OFF_TIMEOUT);
设置系统锁屏的时间
/**
	 * 设置系统锁屏时间 (毫秒)
	 * 
	 * @param context
	 * @param paramInt
	 */
	public void setScreenOffTime(int paramInt) {
		try {
			Settings.System.putInt(getContentResolver(),
					Settings.System.SCREEN_OFF_TIMEOUT, paramInt);
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}

屏幕亮度 代码如下:

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;

public class BrightUtil {
	/**
	 * 获得当前屏幕亮度的模式 SCREEN_BRIGHTNESS_MODE_AUTOMATIC = 1 为自动调节屏幕亮度
	 * SCREEN_BRIGHTNESS_MODE_MANUAL = 0 为手动调节屏幕亮度
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenMode(Context context) {
		int screenMode = 0;
		try {
			screenMode = Settings.System.getInt(context.getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS_MODE);
		} catch (Exception localException) {
		}
		return screenMode;
	}

	/**
	 * 获得当前屏幕亮度值 (0--255)
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenBrightness(Context context) {
		int screenBrightness = 255;
		try {
			screenBrightness = Settings.System.getInt(
					context.getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS);
		} catch (Exception localException) {
		}
		return screenBrightness;
	}

	/**
	 * 设置当前屏幕亮度的模式 SCREEN_BRIGHTNESS_MODE_AUTOMATIC = 1 为自动调节屏幕亮度
	 * SCREEN_BRIGHTNESS_MODE_MANUAL = 0 为手动调节屏幕亮度
	 * 
	 * @param context
	 * @param paramInt
	 */
	public static void changeScreenMode(Context context, int paramInt) {
		try {
			if (paramInt == 0) {
				stopAutoBrightness(context);
			} else {
				startAutoBrightness(context);
			}
			Settings.System.putInt(context.getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS_MODE, paramInt);
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}

	/**
	 * 设置当前屏幕亮度值 0--255
	 * 
	 * @param context
	 * @param paramInt
	 */
	public static void saveScreenBrightness(Context context, int paramInt) {
		try {
			Settings.System.putInt(context.getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS, paramInt);
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}

	/**
	 * 停止自动亮度调节
	 * 
	 * @param context
	 */
	public static void stopAutoBrightness(Context context) {
		final ContentResolver resolver = context.getContentResolver();
		Settings.System.putInt(resolver,
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
		final Uri uri = android.provider.Settings.System
				.getUriFor("screen_brightness");
		resolver.notifyChange(uri, null);
	}

	/**
	 * 开启亮度自动调节
	 * 
	 * @param context
	 */
	public static void startAutoBrightness(Context context) {
		final ContentResolver resolver = context.getContentResolver();
		Settings.System.putInt(resolver,
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
		final Uri uri = android.provider.Settings.System
				.getUriFor("screen_brightness");
		resolver.notifyChange(uri, null);
	}

}

权限如下:

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




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26435次
    • 积分:828
    • 等级:
    • 排名:千里之外
    • 原创:58篇
    • 转载:4篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论