Android Light开发(一)

来源:互联网 发布:淘宝上注册公司靠谱吗 编辑:程序博客网 时间:2024/04/28 04:37

Light源码

public abstract class Light {    public static final int LIGHT_FLASH_NONE = 0;    public static final int LIGHT_FLASH_TIMED = 1;    public static final int LIGHT_FLASH_HARDWARE = 2;    /**     * Light brightness is managed by a user setting.     */    public static final int BRIGHTNESS_MODE_USER = 0;    /**     * Light brightness is managed by a light sensor.     */    public static final int BRIGHTNESS_MODE_SENSOR = 1;    public abstract void setBrightness(int brightness);    public abstract void setBrightness(int brightness, int brightnessMode);    public abstract void setColor(int color);    public abstract void setFlashing(int color, int mode, int onMS, int offMS);    public abstract void pulse();    public abstract void pulse(int color, int onMS);    public abstract void turnOff();}

public abstract class LightsManager {    public static final int LIGHT_ID_BACKLIGHT = 0;//背光灯    public static final int LIGHT_ID_KEYBOARD = 1;//键盘灯    public static final int LIGHT_ID_BUTTONS = 2;//Home Menu,Back键灯    public static final int LIGHT_ID_BATTERY = 3;  //充电灯    public static final int LIGHT_ID_NOTIFICATIONS = 4;//通知灯    public static final int LIGHT_ID_ATTENTION = 5;   //重要灯    public static final int LIGHT_ID_BLUETOOTH = 6;//蓝牙    public static final int LIGHT_ID_WIFI = 7;//WIFI灯    public static final int LIGHT_ID_COUNT = 8;    public abstract Light getLight(int id);}

系统开发点亮灯已充电灯为例

    <!-- Default color for notification LED. -->    <color name="config_defaultNotificationColor">#ffffffff</color>    <!-- Default LED on time for notification LED in milliseconds. -->    <integer name="config_defaultNotificationLedOn">500</integer>    <!-- Default LED off time for notification LED in milliseconds. -->    <integer name="config_defaultNotificationLedOff">2000</integer>    <!-- Default value for led color when battery is low on charge -->    <integer name="config_notificationsBatteryLowARGB">0xFFFF0000</integer>    <!-- Default value for led color when battery is medium charged -->    <integer name="config_notificationsBatteryMediumARGB">0xFFFFFF00</integer>    <!-- Default value for led color when battery is fully charged -->    <integer name="config_notificationsBatteryFullARGB">0xFF00FF00</integer>    <!-- Default value for LED on time when the battery is low on charge in miliseconds -->    <integer name="config_notificationsBatteryLedOn">125</integer>    <!-- Is the notification LED intrusive? Used to decide if there should be a disable option -->    <bool name="config_intrusiveNotificationLed">false</bool>    <!-- Default value for LED off time when the battery is low on charge in miliseconds -->    <integer name="config_notificationsBatteryLedOff">2875</integer>


    private final class Led {        private final Light mBatteryLight;        private final int mBatteryLowARGB;        private final int mBatteryMediumARGB;        private final int mBatteryFullARGB;        private final int mBatteryLedOn;        private final int mBatteryLedOff;        public Led(Context context, LightsManager lights) {            mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY);            mBatteryLowARGB = context.getResources().getInteger(                    com.android.internal.R.integer.config_notificationsBatteryLowARGB);//0xFFFF0000            mBatteryMediumARGB = context.getResources().getInteger(                    com.android.internal.R.integer.config_notificationsBatteryMediumARGB);//            mBatteryFullARGB = context.getResources().getInteger(                    com.android.internal.R.integer.config_notificationsBatteryFullARGB);            mBatteryLedOn = context.getResources().getInteger(                    com.android.internal.R.integer.config_notificationsBatteryLedOn);            mBatteryLedOff = context.getResources().getInteger(                    com.android.internal.R.integer.config_notificationsBatteryLedOff);        }        mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED,                            mBatteryLedOn, mBatteryLedOff);//红灯闪     mBatteryLight.setColor(mBatteryFullARGB);//绿灯亮  mBatteryLight.turnOff();//充电灯关闭


第三方应用点亮灯



//如果硬件支持通知灯 

findViewById(R.id.red_open).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {turnOnLED(Color.RED);}});findViewById(R.id.green_open).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {turnOnLED(Color.GREEN);}});findViewById(R.id.blue_open).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {turnOnLED(Color.BLUE);}});findViewById(R.id.blue_close).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {turnOnLED(Color.YELLOW);}});findViewById(R.id.green_close).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {turnOffLED();}});} private void turnOnLED(int color){  System.out.println("wangruiCIT_LEDTEST START!!!");int ID_LED = 19871103; NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification();notification.ledARGB = color;  //notification.defaults |= Notification.DEFAULT_LIGHTS;    notification.ledOnMS = 1; notification.ledOffMS =0; //notification.ledOnMS = 125; //notification.ledOffMS =2875; notification.flags = Notification.FLAG_SHOW_LIGHTS; nm.notify(ID_LED, notification); }  private void turnOffLED(){  System.out.println("wangruiCIT_LEDTEST START!!!");int ID_LED = 19871103; NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification();notification.ledARGB = Color.BLACK;  //notification.defaults |= Notification.DEFAULT_LIGHTS;    notification.ledOnMS = 0; notification.ledOffMS = 0; notification.flags = Notification.FLAG_SHOW_LIGHTS; nm.notify(ID_LED, notification); }}



或者直接这样,前提是三方应用必须打入系统应用,并且在应用程序的AndroidManifest.xml中的manifest节点中加入android:sharedUserId="android.uid.system"这个属性。

通过系统一起编译

static String RED_LED_DEV = "/sys/class/leds/red/brightness";    static String GREEN_LED_DEV = "/sys/class/leds/green/brightness";    static String BLUE_LED_DEV = "/sys/class/leds/blue/brightness";    static void setLedStatus (String ledDev, boolean setStatusOn) {       try {           FileOutputStream fLed = new FileOutputStream(ledDev);           fLed.write((setStatusOn ? "255" : "0").getBytes());           fLed.close();       } catch (Exception e) {           e.printStackTrace();        }    }    










1 0