Delayed timer at Insertion – ANDROID TIMER

In the industrial World, and in domotics one of the needs that you can find, is how to generate an Event after sometime Interval that you fixed .
In this simple blog we want show you, in a few simple rows of code, a Timer delayed in this insertion using Android Timers and in detail, Executors.

We are using in this case out pLtouch 101g, so you can make your idea real.
Suppose we want to activate a Relè exit after some time T from the presence of a digital input.

All that we need is a method name ScheduledExecutorService; please read this Executors tutorial

int TimeValue = 5; / 5 second of delay for active output
if (pManager.digitalRead(0) == DigitalValues.HIGH) { // if input is HIGH
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);

ScheduledFuture scheduledFuture = scheduledExecutorService.schedule(new Callable() {
	public Object call() throws Exception {
	     pManager.digitalWrite(1,DigitalValues.HIGH); //delay Time TimeValue and active output
	     return "Called!";
			}
		},
	TimeValue,TimeUnit.SECONDS); // TimeValue is time of delay - TimeUnit.SECONDS is time scale Second

	} else{
	   pManager.digitalWrite(1,DigitalValues.LOW); // if input is LOW then switch off output

	}

In our case using the pLtouch 101g we are using the library for the acquisition of digital inputs, e when something present on input the Timer counts 5 seconds(TimeValue)
after that i activates the first Relè exit
When the Input stops coming, i set the Output to zero