Changing color of LED notification every 5 seconds
<p>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.</p>

<pre><code>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();
}
}
</code></pre>

<p>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 :).</p>

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

<p>Thank in advance and sorry for my English.</p>

<p><strong>I can't answer to my own question so I'll add this here:</strong></p>

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

<p>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 )</p>

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

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

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

<p>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 :).</p>

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