ed灯的控制在http://gqdy365.iteye.com/admin/blogs/2208344中写的方法是直接通过操作led设置来控制灯的开关。这样做的问题是未按Android标准结构实现,可能存在潜在的问题,后面分析的一下灯的源码,原来Android系统里面已经提供的相关的实现。
Android系统标准的led可以通过发送通知来控制led灯,做法如下:
通过查看灯的实现源码,发现如下问题:
在来电或者系统屏幕亮的情况下是没办法控制灯的,具体源码(com.android.server.NotificationManagerService)中是这样的:
如果改为屏幕亮的情况可以点亮则将mScreenOn去除
在com.android.server.LightsService中对灯的操作做了封装,所有灯的操作调用了:
其中setLight_native方法是一个本地方法,代码在 frameworks/base/services/jni/com_android_server_LightsService.cpp中。
在com_android_server_LightsService中最终还是通过操作设备文件来实现灯的开关,文件位置如下:
参考资料:
http://blog.csdn.net/u011630458/article/details/22280841
http://blog.csdn.net/u011630458/article/details/22312901
Android系统标准的led可以通过发送通知来控制led灯,做法如下:
- private void notificactionLed() {
- NotificationManager manager = (NotificationManager) this
- .getSystemService(Context.NOTIFICATION_SERVICE);
- Notification notification = new Notification();
- notification.icon = R.drawable.ic_launcher;
- notification.tickerText = "发送灯通知";
- /**
- * To turn the LED off, pass 0 in the alpha channel for colorARGB or 0 for both ledOnMS and ledOffMS.
- To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.
- To flash the LED, pass the number of milliseconds that it should be on and off to ledOnMS and ledOffMS.
- */
- notification.defaults = Notification.DEFAULT_LIGHTS;
- notification.ledARGB = 0xffffffff;//控制led灯的颜色
- //灯闪烁时需要设置下面两个变量
- notification.ledOnMS = 300;
- notification.ledOffMS = 300;
- notification.flags = Notification.FLAG_SHOW_LIGHTS;
- Intent intent = new Intent(this, MainActivity.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
- intent, PendingIntent.FLAG_ONE_SHOT);
- notification.setLatestEventInfo(this, "灯测试", "led灯测试", pendingIntent);
- manager.notify(1, notification);
- }
通过查看灯的实现源码,发现如下问题:
在来电或者系统屏幕亮的情况下是没办法控制灯的,具体源码(com.android.server.NotificationManagerService)中是这样的:
如果改为屏幕亮的情况可以点亮则将mScreenOn去除
- private void More ...updateLightsLocked()
- 2229 {
- 2230 // handle notification lights
- 2231 if (mLedNotification == null) {
- 2232 // get next notification, if any
- 2233 int n = mLights.size();
- 2234 if (n > 0) {
- 2235 mLedNotification = mLights.get(n-1);
- 2236 }
- 2237 }
- 2238
- 2239 // Don't flash while we are in a call or screen is on
- 2240 if (mLedNotification == null || mInCall || mScreenOn) {
- 2241 mNotificationLight.turnOff();
- 2242 } else {
- 2243 final Notification ledno = mLedNotification.sbn.getNotification();
- 2244 int ledARGB = ledno.ledARGB;
- 2245 int ledOnMS = ledno.ledOnMS;
- 2246 int ledOffMS = ledno.ledOffMS;
- 2247 if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) {
- 2248 ledARGB = mDefaultNotificationColor;
- 2249 ledOnMS = mDefaultNotificationLedOn;
- 2250 ledOffMS = mDefaultNotificationLedOff;
- 2251 }
- 2252 if (mNotificationPulseEnabled) {
- 2253 // pulse repeatedly
- 2254 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
- 2255 ledOnMS, ledOffMS);
- 2256 }
- 2257 }
- 2258 }
在com.android.server.LightsService中对灯的操作做了封装,所有灯的操作调用了:
- 116 private void More ...setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) {
- 117 if (color != mColor || mode != mMode || onMS != mOnMS || offMS != mOffMS) {
- 118 if (DEBUG) Slog.v(TAG, "setLight #" + mId + ": color=#"
- 119 + Integer.toHexString(color));
- 120 mColor = color;
- 121 mMode = mode;
- 122 mOnMS = onMS;
- 123 mOffMS = offMS;
- 124 setLight_native(mNativePointer, mId, color, mode, onMS, offMS, brightnessMode);
- 125 }
- 126 }
其中setLight_native方法是一个本地方法,代码在 frameworks/base/services/jni/com_android_server_LightsService.cpp中。
在com_android_server_LightsService中最终还是通过操作设备文件来实现灯的开关,文件位置如下:
参考资料:
http://blog.csdn.net/u011630458/article/details/22280841
http://blog.csdn.net/u011630458/article/details/22312901