android 呼吸灯,指示灯颜色和亮度设置

工作需要,要求改动指示灯的亮度,翻遍了百度,改变指示灯亮度都是直接修改文件/sys/class/leds/green/brightness。

后来自己查看底层代码,其实应该是上层修改的,没必要直接修改底层文件,可以直接复制下面方法使用,如有错误,请不吝指点:

  /*计算指示灯颜色和亮度
    * setColor:灯的颜色,可以设置为:red:0xffff0000 blue:0xff0000ff green:0xff00ff00
    * brightness:灯的亮度,可设置范围0~255
    * */
    private int setSignalLightBrightness(int setColor, int brightness) {
        int color = brightness & 0x000000ff;//假设亮度设为20,20的16进制为14,得0x00000014
        color = 0xff000000 | (color << 16) | (color << 8) | color; //移位和异运算后得0xff141414
        color = color & setColor;  //(假设为绿色)0xff00ff00 & 0xff141414 得0xff001400
        return color;//最终得0xff001400,后面传入底层进一步计算得各颜色亮度值(如下):

        /*上面的值后面传到底层时经过移位运算符计算,得到各颜色的亮度值:
            red = (state->color >> 16) & 0xFF;  //亮度为0
            green = (state->color >> 8) & 0xFF; //亮度为20
            blue = state->color & 0xFF;         //亮度为0
            底层文件参考:hardware/light/lights.c
            */
    }

 

private void showRGBLight(boolean state) {
        try {
            mNM = (NotificationManager) mContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            Log.d(TAG, "showRGBLight mNM is111  " + mNM);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        if (state) {
            n = new Notification();
            n.flags |= Notification.FLAG_SHOW_LIGHTS;
            int color = setSignalLightBrightness(0xff00ff00, 40);//颜色:green,亮度:40
            n.ledARGB = color;//颜色和亮度
            n.ledOnMS = 500;//亮灯时间
            n.ledOffMS = 4000;//灭灯时间
            mNM.notify(309, n); //309:normal light
        } else {
            mNM.cancel(309);
        }
    }
 

 

你可能感兴趣的:(安卓)