Android led灯实现大致流程

来源:互联网 发布:这个男人来自地球 知乎 编辑:程序博客网 时间:2024/04/29 10:33
ed灯的控制在http://gqdy365.iteye.com/admin/blogs/2208344中写的方法是直接通过操作led设置来控制灯的开关。这样做的问题是未按Android标准结构实现,可能存在潜在的问题,后面分析的一下灯的源码,原来Android系统里面已经提供的相关的实现。 

Android系统标准的led可以通过发送通知来控制led灯,做法如下: 

Java代码  收藏代码
  1. private void notificactionLed() {  
  2.     NotificationManager manager = (NotificationManager) this  
  3.             .getSystemService(Context.NOTIFICATION_SERVICE);  
  4.     Notification notification = new Notification();  
  5.     notification.icon = R.drawable.ic_launcher;  
  6.     notification.tickerText = "发送灯通知";  
  7.   
  8.     /** 
  9.      * To turn the LED off, pass 0 in the alpha channel for colorARGB or 0 for both ledOnMS and ledOffMS.  
  10.        To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.  
  11.        To flash the LED, pass the number of milliseconds that it should be on and off to ledOnMS and ledOffMS. 
  12.      */  
  13.     notification.defaults = Notification.DEFAULT_LIGHTS;  
  14.     notification.ledARGB = 0xffffffff;//控制led灯的颜色  
  15.       
  16.     //灯闪烁时需要设置下面两个变量  
  17.     notification.ledOnMS = 300;  
  18.     notification.ledOffMS = 300;  
  19.       
  20.     notification.flags = Notification.FLAG_SHOW_LIGHTS;  
  21.   
  22.     Intent intent = new Intent(this, MainActivity.class);  
  23.     PendingIntent pendingIntent = PendingIntent.getActivity(this0,  
  24.             intent, PendingIntent.FLAG_ONE_SHOT);  
  25.   
  26.     notification.setLatestEventInfo(this"灯测试""led灯测试", pendingIntent);  
  27.     manager.notify(1, notification);  
  28. }  


通过查看灯的实现源码,发现如下问题: 
在来电或者系统屏幕亮的情况下是没办法控制灯的,具体源码(com.android.server.NotificationManagerService)中是这样的: 
如果改为屏幕亮的情况可以点亮则将mScreenOn去除
Java代码  收藏代码
  1. private void More ...updateLightsLocked()  
  2. 2229    {  
  3. 2230        // handle notification lights  
  4. 2231        if (mLedNotification == null) {  
  5. 2232            // get next notification, if any  
  6. 2233            int n = mLights.size();  
  7. 2234            if (n > 0) {  
  8. 2235                mLedNotification = mLights.get(n-1);  
  9. 2236            }  
  10. 2237        }  
  11. 2238  
  12. 2239        // Don't flash while we are in a call or screen is on  
  13. 2240        if (mLedNotification == null || mInCall || mScreenOn) {  
  14. 2241            mNotificationLight.turnOff();  
  15. 2242        } else {  
  16. 2243            final Notification ledno = mLedNotification.sbn.getNotification();  
  17. 2244            int ledARGB = ledno.ledARGB;  
  18. 2245            int ledOnMS = ledno.ledOnMS;  
  19. 2246            int ledOffMS = ledno.ledOffMS;  
  20. 2247            if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) {  
  21. 2248                ledARGB = mDefaultNotificationColor;  
  22. 2249                ledOnMS = mDefaultNotificationLedOn;  
  23. 2250                ledOffMS = mDefaultNotificationLedOff;  
  24. 2251            }  
  25. 2252            if (mNotificationPulseEnabled) {  
  26. 2253                // pulse repeatedly  
  27. 2254                mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,  
  28. 2255                        ledOnMS, ledOffMS);  
  29. 2256            }  
  30. 2257        }  
  31. 2258    }  


在com.android.server.LightsService中对灯的操作做了封装,所有灯的操作调用了: 
Java代码  收藏代码
  1. 116        private void More ...setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) {  
  2. 117            if (color != mColor || mode != mMode || onMS != mOnMS || offMS != mOffMS) {  
  3. 118                if (DEBUG) Slog.v(TAG, "setLight #" + mId + ": color=#"  
  4. 119                        + Integer.toHexString(color));  
  5. 120                mColor = color;  
  6. 121                mMode = mode;  
  7. 122                mOnMS = onMS;  
  8. 123                mOffMS = offMS;  
  9. 124                setLight_native(mNativePointer, mId, color, mode, onMS, offMS, brightnessMode);  
  10. 125            }  
  11. 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
0 0
原创粉丝点击