Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top