Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have LED Light Notification?

Tags:

android

Update: I am reworking the original post for compatibility before Android 3.0.

I am trying to create a simple notification and everything but the light works great. I do have the screen off when the notification fires. Using this deprecated code sound and vibrations works on Android 4.0 (Galaxy Nexus) and Android 2.3 (HTC EVO).

  • On the 2.3 HTC EVO the lights also work.
  • On the 4.0 Galaxy Nexus the lights do not work.

    Notification notification = 
            new Notification(R.drawable.ic_launcher, "My Ticker!",System.currentTimeMillis());
    notification.setLatestEventInfo(context, "My Title", "My Message", pendingIntent);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    
    //notification.defaults |= Notification.DEFAULT_LIGHTS;
    
    notification.ledARGB = 0xff00ff00;
    notification.ledOnMS = 300;
    notification.ledOffMS = 1000;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    

I have also tried newer APIs that are not part of the v4 compatibility library, so I could only test this on the Galaxy Nexus. Vibration and Sound works again but not lights.

Notification.Builder builder = new Notification.Builder(context);
        builder.setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("My Ticker")
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
            .setLights(0xff00ff00, 300, 100)
            .setContentTitle("My Title 1")
            .setContentText("My Text 1");
        Notification notification = builder.getNotification();

I have now tested this on two stock Galaxy Nexus phones and the light works on neither. The screen IS off when I run the tests and all other apps on the phones trigger the lights without issue.

like image 585
Cameron McBride Avatar asked May 01 '12 18:05

Cameron McBride


People also ask

Is there a LED light notification?

In some Android smartphones, the notification LED light's behavior could be customized per app, so that, each color would indicate a different app. Apps like WhatsApp or Telegram also include a setting to set this color for the LED light.


2 Answers

If you use setDefaults(DEFAULT_LIGHTS) or setDefaults(DEFAULT_ALL), it will ignore FLAG_SHOW_LIGHTS and any calls to setLights(). I found that on my phone, the defaults would not light up anything. So do not use DEFAULT_ALL or DEFAULT_LIGHTS, and play with the RGB color if necessary... although 0xff00ff00 should work in pretty much all cases.

like image 169
Drake Clarris Avatar answered Oct 21 '22 17:10

Drake Clarris


Came across this today..if its still useful... Only certain notifications with a priority - HIGH,MAX will be able to cause the LED to glow, while those with lower priority LOW,MIN should not. Adding this should make the LED glow...

builder.setPriority(Notification.PRIORITY_MAX)

like image 33
bipin Avatar answered Oct 21 '22 19:10

bipin