0

I've created a button to trigger a pulse LED action on my app, but I cant make it pulse forever. It always Pulses by value ( on the example above you will see it pulses 10 times and then stops )

//Button Pulse
Button bpulse = (Button) findViewById(R.id.bpulse);
bpulse.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
    // TODO Auto-generated method stub                  
    Intent led = new Intent(IlluminationIntent.ACTION_START_LED_PULSE);

    led.putExtra(IlluminationIntent.EXTRA_PACKAGE_NAME, "com.devsgonemad.xslc");

    led.putExtra(IlluminationIntent.EXTRA_LED_ID,
                     IlluminationIntent.VALUE_BUTTON_2);

    led.putExtra(IlluminationIntent.EXTRA_LED_NO_OF_PULSES, 10);

    led.putExtra(IlluminationIntent.EXTRA_LED_PULSE_ON_TIME, 1000);

    led.putExtra(IlluminationIntent.EXTRA_LED_PULSE_OFF_TIME, 1000);

    led.putExtra(IlluminationIntent.EXTRA_LED_COLOR, m_ledColor);

    startService(led);

    m_isEnabled = true;
}

How can I make this pulse forever when the button is pressed until the user goes back to the app and stops it from pulsing?

Best regards

Sean Mickey
  • 7,618
  • 2
  • 32
  • 58
GeeCode
  • 21
  • 2
  • 10
  • 1
    post he code in your service. – FoamyGuy May 07 '12 at 14:56
  • Where is your pulsing code and what is the problem you are facing? – Gaurav Agarwal May 07 '12 at 14:56
  • thats the code right there that triggers the pulse action of the LEDs, I am using an external jar file from Sony that allows me to control the LEDs. My problem is I want that Pulse effect to stay active when the user presses the button, at the moment, it does how many pulses like above on the specified . – GeeCode May 07 '12 at 15:41

1 Answers1

1

How about using a while loop? You could put the number of pulses as 1, and let this run continuously in a while loop, until some condition (in your case some button is pressed) is satisfied.

while(buttonNotPressed){
  led.putExtra(IlluminationIntent.EXTRA_LED_NO_OF_PULSES, 1);
  startService(led);
}

Ofcourse this has an obvious pitfall that you may start many parallel services all trying to flash the LED once. But I guess you can control your while loop to wait for the service to end before you start the next iteration.
EDIT: For the while loop to wait for your service, you will need to add some sort of flag inside your service on which the while loop can wait. Once the service finishes, the flag can be set and the while loop can move onto the next iteration, thus giving you infinite rounds of flashes until the button is pressed.

Urban
  • 2,201
  • 24
  • 52
  • That sounds like a good method to me, If I only would understand how to implement it ;) This is my 2nd week learning and trying Java out, I never had any programming experiences before, So we can say I am pretty much a Newbie ;) – GeeCode May 07 '12 at 15:43
  • Can you please tell me more about that flag you mentioned earlier ? – GeeCode May 07 '12 at 21:17
  • Well there are multiple ways of implementing that, and it all depends on how youve written your service. The basic idea is to somehow have the while loop know when to start another service (only after one service has ended). So you could either have one global integer (a flag) that is set when the service ends. Or you could have one integer value in your shared preferences which can act as a flag. Now to wait for this variable, you'll need another while loop inside the 1st one. Something like: `while(myFlag){sleep(1000);}` i.e: Sleep for a second if the the flag isnt set. – Urban May 08 '12 at 02:06
  • Now if you plan to use this, make sure you have a separate thread in which this while loop sleeps. If you make the main UI thread sleep, then the program will become unresponsive for the time its sleeping. I would also suggest that you try the code I had put in the answer without worrying abt flags (as in let many parallel services be created) and see what happens. You might not even need your while loop to wait. Do try that and tell me what happens. – Urban May 08 '12 at 02:09
  • Ok so I am now extremely confused, I just had my Exams today, been working on that the whole week, and now i got a little bit more time to dedicate to this hobby, So you mean i Should just the While {} between my code up there? with this in between?: led.putExtra(IlluminationIntent.EXTRA_LED_NO_OF_PULSES, 1); startService(led); how can i say buttonNotPressed ? bstop.NotPressed ? – GeeCode May 08 '12 at 10:52
  • Alright, you seem to be getting confused. I'll start from the top. Firstly, the `buttonNotPressed` was just some variable name I had chosen. It isnt a pre-defined attribute, it is a flag that you have to set yourself. So how will you set it? Well the stop button will have an onClick method. Inside this onClick set the flag. The while loop will stop when it notices this flag has been set by the onCLick of the stop and this will stop blinking the flash. Also, this whole while loop will need to be inside a background thread. – Urban May 08 '12 at 11:10
  • It needs to be inside a background thread as otherwise youll block your UI thread, thus making your app not responsive. As you mentioned you are a beginner programmer, I strongly suggest you not to start with such difficult application. You must first get yourself fluent in JAVA and then build some basic Android apps before you can attempt to make the more difficult apps, like this one. – Urban May 08 '12 at 11:12
  • You are probably right, it took me like 2 straight days to get the "basic" functions of my app to work, it has been an amazing challenge so far, and it now starts to make sense what you explained me, I can see me fighting with it ;). Still I want to just make this app functional for my daily use and then start a simpler app just like you said to get fluent with java language. I really appreciate your help mate. Best regards – GeeCode May 08 '12 at 17:01
  • Glad I could help. Keep playing around with it and youre sure to learn lots while you have fun. And if you get stuck, were here to help :) – Urban May 08 '12 at 17:33