---
title: "Android 闪光效果"
author: "80demon"
date: "2011-11-12T00:31:00Z"
tags: []
slug: "android"
draft: false
lastmod: "2011-11-18T14:38:45Z"
id: 1265746
url: "https://80demon.iteye.com/blog/1265746"
---

Notification也包含属性来设置手机LED的颜色和闪烁频率。

ledARGB属性用于设置LED的颜色，而ledOffMS和ledOnMS属性用来设置LED闪烁的频率和样式。你可以设置ledOnMS属性为1，ledOffMS属性为0来让LED始终亮着；或者将两者设置为0来将LED关闭。一旦你设置了LED的设定，你也必须为Notification的flags属性添加FLAG\_SHOW\_LIGHTS标志位。

接下来的代码片段显示了如何将点亮红色的LED：

**java代码：**  
notification.ledARGB = Color.RED;  
notification.ledOffMS = 0;  
notification.ledOnMS = 1;  
notification.flags = notification.flags | Notification.FLAG\_SHOW\_LIGHTS;

控制颜色和闪烁频率是为向用户传递信息的另一种途径。

在地震监视的例子中，使用设备的LED来辅助传达级别，帮助用户从颜色上来感受地震的级别。在下面的片段中，LED的颜色取决于地震的级别，而闪烁的频率相反地关联于地震的影响：

**java代码：**  
int color;

if (quake.getMagnitude() &lt; 5.4)  
color = Color.GREEN;

else if (quake.getMagnitude() &lt; 6)  
color = Color.YELLOW;

else  
color = Color.RED;

newEarthquakeNotification.ledARGB = color;  
newEarthquakeNotification.ledOffMS = (int)vibrateLength;  
newEarthquakeNotification.ledOnMS = (int)vibrateLength;  
newEarthquakeNotification.flags = newEarthquakeNotification.flags |Notification.FLAG\_SHOW\_LIGHTS;