Search code examples
javaandroidledflashlight

Controling Android's front led light


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.


Solution

  • 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 );
    }