Toggle navigation

鸿蒙OS Light开发指导

场景介绍

当设备需要设置不同的闪烁效果时,可以调用 Light 模块,例如,LED 灯能够设置灯颜色、灯亮和灯灭时长的闪烁效果。

说明

使用该功能依赖于硬件设备是否具有 LED 灯。

接口说明

灯模块主要提供的功能有:查询设备上灯的列表,查询某个灯设备支持的效果,打开和关闭灯设备。LightAgent 类开放能力如下,具体请查阅 API 参考文档。

接口名描述
getLightIdList()获取硬件设备上的灯列表。
isSupport(int)根据指定灯Id查询硬件设备是否有该灯。
isEffectSupport(int, String)查询指定的灯是否支持指定的闪烁效果。
turnOn(int, String)对指定的灯创建指定效果的一次性闪烁。
turnOn(int, LightEffect)对指定的灯创建自定义效果的一次性闪烁。
turnOn(String)对指定的灯创建指定效果的一次性闪烁。
turnOn(LightEffect)对指定的灯创建自定义效果的一次性闪烁。
turnOff(int)关闭指定的灯。
turnOff()关闭指定的灯。

开发步骤

  1. 查询硬件设备上灯的列表。

  1. 查询指定的灯是否支持指定的闪烁效果。

  1. 创建不同的闪烁效果。

  1. 关闭指定的灯。

   private LightAgent lightAgent = new LightAgent();

    
   @Override
   public void onStart(Intent intent) {
       super.onStart(intent);
       super.setUIContent(ResourceTable.Layout_light_layout);

    
       // ...

    
       // 查询硬件设备上的灯列表
       List<Integer> myLightList = lightAgent.getLightIdList();
       if (myLightList.isEmpty()) {
           return;
       }
       int lightId = myLightList.get(0);

    
       // 查询指定的灯是否支持指定的闪烁效果
       boolean isSupport = lightAgent.isEffectSupport(lightId, LightEffect.LIGHT_ID_KEYBOARD);

    
       // 创建指定效果的一次性闪烁
       boolean turnOnResult = lightAgent.turnOn(lightId, LightEffect.LIGHT_ID_KEYBOARD);

    
       // 创建自定义效果的一次性闪烁
       LightBrightness lightBrightness = new LightBrightness(255, 255, 255);
       LightEffect lightEffect = new LightEffect(lightBrightness, 1000, 1000);
       boolean turnOnEffectResult = lightAgent.turnOn(lightId, lightEffect);

    
       // 关闭指定的灯
       boolean turnOffResult = lightAgent.turnOff(lightId);
   }