En mi vida de desarrollador ANDROID me he topado con la necesidad de encender y apagar el LED de notificaciones que poseen algunos celulares, con el fin de demostrar una carga de datos correcta (LED en Verde) o una carga erronea (LED en Rojo).
Al no encontrar casi documentación sobre este tema, decidi idear mi propia solución y quiero compartir la forma en que yo trabajo con este LED.
//Led en Verde private void GreenFlashLight() { String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) act.getSystemService(ns); Notification notification = new Notification(R.drawable.check_acept, "", System.currentTimeMillis()); notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL; //Indico el color del led en verde notification.ledARGB = Color.GREEN; //Indico que el Notification Led se encienda por tiempo indeterminado notification.ledOnMS = 1; Context context = act.getApplicationContext(); CharSequence contentTitle = "Producto Escaneado"; CharSequence contentText = "Correcto!"; Intent notificationIntent = new Intent(act, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(act, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); //Actualizo el estado del LED mNotificationManager.notify(1, notification); //Espero 20 mili segundos con el led prendido try{ Thread.sleep(200); }catch(InterruptedException e){} //Función que apaga el LED OffLed(); } //Led en Rojo private void RedFlashLight() { String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) act.getSystemService(ns); Notification notification = new Notification(R.drawable.check_acept, "", System.currentTimeMillis()); notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL; //Establesco el color del LED en Rojo notification.ledARGB = Color.RED; //Enciendo el LED por tiempo Indeterminado notification.ledOnMS = 1; Context context = act.getApplicationContext(); CharSequence contentTitle = "Producto Escaneado"; CharSequence contentText = "Correcto!"; Intent notificationIntent = new Intent(act, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(act, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); //Actualizo el estado del LED mNotificationManager.notify(1, notification); try{ Thread.sleep(200); }catch(InterruptedException e){} //Funcion que apaga el LED OffLed(); } //Apago el Led private void OffLed(){ String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) act.getSystemService(ns); Notification notification = new Notification(R.drawable.check_acept, "Hello", System.currentTimeMillis()); notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL; //Elimino el Color del LED notification.ledARGB = 0; //Elimino tiempo de encendido del LED notification.ledOnMS = 0; //Elimino tiempo de apagado del LED notification.ledOffMS = 0; Context context = act.getApplicationContext(); CharSequence contentTitle = "Producto Escaneado"; CharSequence contentText = "Correcto!"; Intent notificationIntent = new Intent(act, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(act, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); //Actualizo el estado del LED mNotificationManager.notify(1, notification); }
0 comentarios:
Publicar un comentario