对于平板来说,设置背光对于省点来说比较重要。本文从应用层到内核层讲解系统是如何成功的设置背光的。设置背光,本质上是调节LCD的电压,达到对亮度的控制,Framework层的一系列的接口都是kernel层gpio操作的层层封装和相应的管理。
对于app来说亮度的控制如下:
/*
* 关闭自动调节亮度,改为手动调节亮度
*/
public static void stopAutoBrightness(Activity activity)
{
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
/*
* 开启自动调节亮度
*/
public static void startAutoBrightness(Activity activity)
{
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
/*
* 设置屏幕亮度
*/
public boolean setScreenBrightness(Activity activity, int light)
{
try
{
Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, light);
}
catch (Exception localException)
{
localException.printStackTrace();
return false;
}
Window localWindow = activity.getWindow();
WindowManager.LayoutParams localLayoutParams = localWindow.getAttributes();
float f = light / 255.0F;
localLayoutParams.screenBrightness = f;
localWindow.setAttributes(localLayoutParams);
return true;
}
调用以上几个函数,需要在AndroidManifest.xml中添加下面的权限
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
在调用setScreenBrightness之前,需要用调用stopAutoBrightness,将亮度设置为手动模式。
说到这里,有个小小的问题,就算设置亮度为0,为什么屏幕没变黑,原因是在framework层,在设置亮度时,最低的亮度值是30。
在Framework层,Window.java的setAttributes函数如下:
public void setAttributes(WindowManager.LayoutParams a) {
mWindowAttributes.copyFrom(a);
if (mCallback != null) {
mCallback.onWindowAttributesChanged(mWindowAttributes);
}
}
这里面调用了一个mCallback.onWindowAttributesChanged(mWindowAttributes); 来实现。这里面的mCallback是Window.java的一个接口类CallBack,这个CallBack包含了如下接口:
public interface Callback{
/**
* Called to process key events. At the very least your
* implementation must call
* {@link android.view.Window#superDispatchKeyEvent} to do the
* standard key processing.
*
* @param event The key event.
*
* @return boolean Return true if this event was consumed.
*/
public boolean dispatchKeyEvent(KeyEvent event);
/**
* Called to process a key shortcut event.
* At the very least your implementation must call
* {@link android.view.Window#superDispatchKeyShortcutEvent} to do the
* standard key shortcut processing.
*
* @param event The key shortcut event.
* @return True if this event was consumed.
*/
public boolean dispatchKeyShortcutEvent(KeyEvent event);
/**
* Called to process touch screen events. At the very least your
* implementation must call
* {@link android.view.Window#superDispatchTouchEvent} to do the
* standard touch screen processing.
*
* @param event The touch screen event.
*
* @return boolean Return true if this event was consumed.
*/
public boolean dispatchTouchEvent(MotionEvent event);
/**
* Called to process trackball events. At the very least your
* implementation must call
* {@link android.view.Window#superDispatchTrackballEvent} to do the
* standard trackball processing.
*
* @param event The t