Android led灯实现大致流程

本文介绍了在Android系统中如何使用标准API控制LED灯的开关及闪烁,并分析了底层实现原理,指出在特定条件下(如来电或屏幕亮起)无法正常工作的原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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中最终还是通过操作设备文件来实现灯的开关,文件位置如下:

[img]http://dl2.iteye.com/upload/attachment/0108/4915/92728da8-3488-3c04-820b-509ebd472d8d.png[/img]


参考资料:
http://blog.youkuaiyun.com/u011630458/article/details/22280841
http://blog.youkuaiyun.com/u011630458/article/details/22312901
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值