led灯的控制在http://gqdy365.iteye.com/admin/blogs/2208344中写的方法是直接通过操作led设置来控制灯的开关。这样做的问题是未按Android标准结构实现,可能存在潜在的问题,后面分析的一下灯的源码,原来Android系统里面已经提供的相关的实现。

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)中是这样的:

private void More ...updateLightsLocked()2229    {2230        // handle notification lights2231        if (mLedNotification == null) {2232            // get next notification, if any2233            int n = mLights.size();2234            if (n > 0) {2235                mLedNotification = mLights.get(n-1);2236            }2237        }22382239        // Don't flash while we are in a call or screen is on2240        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 repeatedly2254                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

更多相关文章

  1. android sqlite 介绍
  2. Android[高级教程] 设计模式之七 单例模式
  3. Android(安卓)Jni代码示例讲解
  4. android 中 AppWidget 的 ListView 的实现
  5. android的surfaceview的用法
  6. Android(安卓)AIDL 双向调用的使用及相关原理
  7. Android面试知识点总结-Android篇
  8. Android(安卓)Service
  9. 通用(任何android机型)Root教程(完整版!附砖机自救方法)

随机推荐

  1. [实战]MVC5+EF6+MySql企业网盘实战(27)—
  2. 如何在SQL查询中显示特定范围的数字
  3. 从零开始搭建框架SSM+Redis+Mysql(二)之MAV
  4. MySQL中find_in_set的用法(某个字段包含某
  5. mysql-proxy实现读写分离
  6. 保存在Java桌面应用程序应用程序和网站上
  7. PHP : mysqli【面向对象】操作数据库【连
  8. 通过PHP运行CREATE TABLE查询
  9. Mysql 增加外键,删除主外键关联表
  10. Python学习笔记之MySql数据库(一)