Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing color of LED notification every 5 seconds

Tags:

android

I'm trying to turn on the LED notification, for example the green color. Turn the screen off, and every 5 seconds change that color to red (green->red, red->green). I think I've done everything: in a service I created a timer which executes method to display notification.

public class LEDService extends Service
{
private boolean TimerStarted;
private Timer timer;
private NotificationManager myNotificationManager;
private long LastColor;

public TurnLedOn()
{
     Notification notify = new Notification();
     notify.flags |= Notification.FLAG_SHOW_LIGHTS;
     notify.LedOnMS = 500;
     notify.LedOffMS = 0;
     //I in other example I also used array of colors
     if (LastColor == 0x00FF00) notify.LedARGB = 0xFF0000; else notify.LedARGB = 0x00FF00;         
     LastColor = notify.LedARGB;
}

private MyTimerTask extends TimerTask
{
    @Override
    public void run()
    {
        TurnLedOn();
    }
}

@Override
public void OnCreate()
{
    TimerStarted = false;
    myNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) 
{
    if (TimerStarted == false)
    {
        timer = new Timer();
        timer.schedule(new MyTimerTask(), 0, 5000);
    }        
    return START_STICKY;
}

@Override
public IBinder onBind()
{
    return null;
}

@Override
public void onDestroy()
{
    timer.close();
}
}

What is the problem? The LED doesn't change its color... When I turn the screen off, the color is green until I turn on the screen and turn it off again. I want to start this service once, turn off the screen and see the LED light changing color :).

BTW: I tested my phone and it CAN show green and red lights so it's not the problem.

Thank in advance and sorry for my English.

I can't answer to my own question so I'll add this here:

Thanks for your suggestions Olsavage, I added clearAll() to my code, but effect is still the same ;/. I also added logging to my application (Log.i() ). It looks like the system is stopping my service when I turn the screen off (why??). It is something this way:

Screen Turned On: The timer is running and notification is deployed (but I can't see led on, because to see it i have to turn the screen off :P )

Click on the lock button: The timer is almost stopped, so the LED sometimes changes color once.

Screen is turned Off: The timer is not working, TurnLedOn method is not running anymore. LED doesn't change colors.

Now the question is: why my service is stopped after turning the screen off? Even when I am doing simple operation in it (like incrementing a variable). Maybe I have to set its priority or something?

I changed the timer interval to 100ms and see that code is good. The LED changed colors for 5-15 times but then immediately stopped. I know that this application is completely useless but I just want to let it working :).

EDIT2: I think I will have to use AlarmManager and PendingIntent to launch my service... Tomorrow I will try to do that.

like image 407
krzysnick Avatar asked Nov 07 '11 18:11

krzysnick


People also ask

How do I change LED notification color?

To change the colour, open the app, then go to the app's settings menu to find out which options are available. You can turn LED notifications on or off in the “Settings” menu.

How do I change the LED notification on my Android?

If the LED indicator behavior does not work as intended, check the LED notification settings. Follow these steps to turn on LED notification: Navigate to Settings > Display & gestures > Notification light. Tap the setting to turn it On.


1 Answers

Okay, I think I figured it out. Removed the timer at all. Instead I use AlarmManager :).

I added to onCreate following code:

alarmmgr = (AlarmManager) getSystemService(ALARM_SERVICE);

in onStartCommand:

public int onStartCommand(Intent intent, int flags, int startId) 
{
 TurnLedOn();   //Turn LED On
 myPendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent. FLAG_UPDATE_CURRENT);   
 alarmmgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, myPendingIntent); //Setting alarm to be off after 5seconds.
 return Service.START_NOT_STICKY;
}

in onDestroy:

public void onDestroy() 
{
notifymgr.cancel(LED_NOTIFICATION_ID);  //Clearing notification
alarmmgr.cancel(myPendingIntent);   //Clear alarm
}

I think code is ok but I'm totally beginner in Android programming. I think I solved my problem.

like image 120
krzysnick Avatar answered Oct 17 '22 05:10

krzysnick