5

I'm trying to implement a 1 second red blinking at the front led whenever the user presses a certain button. But I'm having trouble finding documentation, tutorials or even code-examples on how to access and work with the front led (by that I mean the led located adjacent to the "selfie" camera and the touch screen).

I've seen examples to work with the flashlight and Camera class (deprecated) but I believe that's not what I'm looking for.

elanonrigby
  • 479
  • 1
  • 6
  • 14

2 Answers2

3

There is no direct access to the front L.E.D. You can schedule Notifications with custom colours for the L.E.D.

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) // To change Light Colors
        .setContentTitle("My Title 1")
        .setContentText("My Text 1");
    Notification notification = builder.getNotification();

For Example For Red Flash of L.E.D.:

private void RedFlashLight()
{
NotificationManager nm = ( NotificationManager ) getSystemService( 
NOTIFICATION_SERVICE );
Notification notif = new Notification();
notif.ledARGB = 0xFFff0000;
notif.flags = Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 100; 
notif.ledOffMS = 100; 
nm.notify(LED_NOTIFICATION_ID, notif);
// Program the end of the light :
mCleanLedHandler.postDelayed(mClearLED_Task, LED_TIME_ON );
}
Hemant Singh
  • 924
  • 1
  • 6
  • 13
  • However, some methods (like setDefaults, getNotification) are deprecated. Should I use them even so? What is the difference of using Notification.Builder and NotificationManager? – elanonrigby Feb 12 '18 at 12:50
  • NotificationManager is the latest API. – Hemant Singh Feb 12 '18 at 13:17
  • what is ' mCleanLedHandler.postDelayed(mClearLED_Task, LED_TIME_ON ); ' ? Could you elaborate this line, please? – elanonrigby Feb 12 '18 at 14:27
  • With mCleanLedHandler being just a handler, and LED_TIME_ON the flash duration. Some notes : 1) Don't let a handler alive when changing activity ! On the OnPause of your activity, clear the handler : mCleanLedHandler.removeCallbacks( mClearLED_Task ); 2) programming the LED suffers one issue : the emulator does not emulate the LED at all... So you have to use a real device to see what it looks like ( but anyway, you always use real device at some point, don't you ? :) ) – Hemant Singh Feb 12 '18 at 17:14
2

The font light is called the Android Notification LED, which not all phones have, and the only way to control it is through launching a notification. There are some tutorials online showing how to do this such as: http://androidblogger.blogspot.co.uk/2009/09/tutorial-how-to-use-led-with-android.html

Anthony Cannon
  • 1,245
  • 9
  • 20