Android O按键背光功能

前言:客户需要在android 8.0上实现按键背光功能,我记得在android 5.1上只需要设置一个config即可(config_button_light_enabled),但是框架变化太大,config找不到了,只能询问高通。然后,高通说8.0没做……(Google did not add interface for button backlight
need you release it from L to O.)

自己动手,丰衣足食,开始移植。
按键监听还是在PhoneWindowManager啦~
初始化:

/** {@inheritDoc} */
    @Override
    public void init(Context context, IWindowManager windowManager,
            WindowManagerFuncs windowManagerFuncs) {
            ...
            mLightsManager = LocalServices.getService(LightsManager.class);
        mLight = mLightsManager.getLight(LightsManager.LIGHT_ID_BUTTONS);
        mButtonBrightness = 2;
        mButtonLightTimeout = 4000;
            ...
}

主要实现方法:

private void setButtonLightEnabled(boolean on) {
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
        mHandler.removeMessages(MSG_BBL_TIMEOUT);
        if (on) {
            mLight.setBrightness(mButtonBrightness);

            mHandler.sendMessageDelayed(
                    mHandler.obtainMessage(MSG_BBL_TIMEOUT),
                    mButtonLightTimeout);
        } else {
            mLight.setBrightness(0);
        }
    }

handler里面添加自动灭灯的流程:

case MSG_BBL_TIMEOUT:
    mLight.setBrightness(0);
        break;

其他定义的地方就不详细写了。

你可能感兴趣的:(android_system)