Color change LED notification every 5 seconds

I am trying to turn on an LED notification, such as green. Turn off the screen, and every 5 seconds, change the color to red (green - red, red - green). I think I did everything: in the service, I created a timer that executes a method to display a 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 does not change color ... When I turn off the screen, the color turns 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 color of the LED backlight :).

By the way: I checked my phone and it MAY show green and red lights so that it is not a problem.

Thanks in advance and sorry for my English.

I cannot answer my question, so add this here:

Thanks for your Olsavage suggestions, I added clearAll () to my code, but the effect is the same; /. I also added an entry to my application (Log.i ()). It seems that the system stops my service when the screen turns off (why?) . This is true:

The screen is on: The timer is running and the notification is maximized (but I can’t see that it is on, because to see it I have to turn off the screen: P)

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

Screen off: The timer does not work, the TurnLedOn method no longer works. LED does not change color.

Now the question is: why is my service stopped after turning off the screen ? Even when I perform a simple operation in it (for example, increasing a variable). Maybe I need to set priority or something else?

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

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

+6
source share
2 answers

Well, I think I figured it out. Completely disabled the timer. Instead, I use AlarmManager :).

I added the following code to onCreate:

 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 the code is fine, but I am completely new to Android programming. I think I solved my problem.

+2
source

You need to use the ARGB format. Try setting 0xFFFF0000 - red and 0xFF00FF00 - green. Hope this helps.

Hmm, maybe your old notification has not cleared? Try using myNotificationManager.clearAll (); Before myNotificationManager.notify (0, notify);

Also try setting notify.ledOffMS = 5000;

+1
source

Source: https://habr.com/ru/post/900965/


All Articles