呼吸灯是个非常酷的功能,闲暇之余就尝试做了这个。实现原理:利用android的通知指示灯,通过闹钟的方式实现呼吸灯。

  俗话说“巧妇难为无米之炊”,我们的呼吸灯在那里呢?哈哈,就用android手机的通知指示灯。如何让led灯亮起来呢。利用发送通知的方式,相关代码如下:

  private void showNotification() {  LogTool.d(TAG, "showNotification....");  if (mNotificationManager == null) {  mNotificationManager = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);  }  mNotificationManager.cancel(NOTIFICATION_ID);  Notification notice = new Notification();  notice.ledARGB= 0xff00ff00;  notice.ledOnMS = 1;  notice.ledOffMS = 0;  notice.flags |= Notification.FLAG_SHOW_LIGHTS;  Intent intent = new Intent();  PendingIntent des = PendingIntent.getActivity(this,  NOTIFICATION_REQUEST_CODE, intent,  PendingIntent.FLAG_CANCEL_CURRENT);  notice.setLatestEventInfo(this, null, null, des);  mNotificationManager.notify(NOTIFICATION_ID, notice);  }
  首先申请系统服务Activity.NOTIFICATION_SERVICE获得NotificationManager,然后创建一个不显示的Notification。让我们看看通知的相关参数:   // 指示灯闪烁   notice.flags |= Notification.FLAG_SHOW_LIGHTS;   //指示灯长亮   notice.ledOnMS = 1;   notice.ledOffMS = 0;   // 配置指示灯的颜色   notice.ledARGB= 0xff00ff00;   这里设置的指示灯的颜色为绿色,可以设置各种颜色实现五颜六色的led呼吸灯效果。   如何设置led灯有规律的闪烁呢。利用timer吗?,错了。在android手机锁屏后,Timer的就开始“墨迹”,定时效果不靠谱。理想的情况是使用闹铃功能,实现如下:
  private void scheduleAlarm() {  LogTool.d(TAG, "scheduleAlarm....");  if (mAlarmManager == null) {  mAlarmManager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);  }  // schedule notification  Intent intent = new Intent(INTENT_ACTION_SCHEDULE);  PendingIntent clock = PendingIntent.getBroadcast(this,  NOTIFICATION_REQUEST_CODE, intent,  PendingIntent.FLAG_CANCEL_CURRENT);  mAlarmManager  .setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,  SystemClock.elapsedRealtime(),  NOTIFICATION_ALARM_LENGTH, clock);  // schedule cancel notification  Intent cancelIntent = new Intent(INTENT_ACTION_CANCEL);  PendingIntent cancel = PendingIntent.getBroadcast(this,  NOTIFICATION_REQUEST_CODE, cancelIntent,  PendingIntent.FLAG_CANCEL_CURRENT);  mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,  SystemClock.elapsedRealtime() + NOTIFICATION_LED_ONMS,  NOTIFICATION_ALARM_LENGTH, cancel);  }
  首先我们获得了系统的闹钟服务Activity.ALARM_SERVICE,然后按固定间隔发送开启和关闭led的广播。   谁来收听我们的“天籁之音”呢?没有知音我们就创造一个:
  private void startReceiver() {  if (mReceiver == null) {  mReceiver = new BroadcastReceiver() {  @Override  public void onReceive(Context context, Intent intent) {  if (intent.getAction().equals(INTENT_ACTION_SCHEDULE)) {  showNotification();  } else if (intent.getAction().equals(INTENT_ACTION_CANCEL)) {  LogTool.d(TAG, "cancel Notification....");  if (mNotificationManager != null) {  mNotificationManager.cancelAll();  }  }  }  };  IntentFilter filter = new IntentFilter();  filter.addAction(INTENT_ACTION_CANCEL);  filter.addAction(INTENT_ACTION_SCHEDULE);  registerReceiver(mReceiver, filter);  }  }


点击此处下载源码


转载:http://www.adobex.com/android/source/details/00000237.htm

更多相关文章

  1. Android(安卓)中的观察者模式Observer
  2. Android(安卓)Notification 详解
  3. 开发者必看|Android(安卓)8.0 新特性及开发指南
  4. Android(安卓)判断应用程序获取通知栏权限是否开启,以及如何跳转
  5. Android(安卓)NotificationManager 和Notification的使用总结
  6. Android(安卓)通知栏显示与设置
  7. Android(安卓)后台服务简要概述
  8. Android(安卓)通知使用权(NotificationListenerService)的使用
  9. Android第十三期 - 百度云推送(百度后台版)

随机推荐

  1. ANDROID Porting系列一、ANDROID编译系统
  2. Android shape图形
  3. Android Drawable Resources系列6:
  4. 13、Android的多线程与异步任务
  5. android 中文粗体
  6. Android文件的读写
  7. android studio 在线更新android sdk,遇到
  8. Android PULL解析XML
  9. android gps开发
  10. 点击事件