Search code examples
javaandroidtimerled

Create SOS signal using Java.Util.Timer


I want to create an android app that performs the SOS signal using the flashlight on the phone and I know how to control the flash light.

This is what I want the flash light to do:

1) Flash light on for 1 second
2) Flash light off for 1 second
3) Flash light on for 1 second
4) Flash light off for 1 second
5) Flash light on for 1 second
6) Flash light off for 1 second

7) Flash light on for 3 seconds
8) Flash light off for 1 second
9) Flash light on for 3 seconds
10) Flash light off for 1 second
11) Flash light on for 3 seconds
12) Flash light off for 1 second

13) Flash light on for 1 second
14) Flash light off for 1 second
15) Flash light on for 1 second
16) Flash light off for 1 second
17) Flash light on for 1 second
18) Flash light off for 1 second

How can I accomplish this using the timer class in java?


Solution

  • The best approach for this would be to create a class with all definitions of the alphabet and their equivalent in Morse Code. Numbers can be added as well. That way you can even enter any text, not just SOS.

    If SOS is your only goal, then creating a class might be an overkill but still good practice.

    Have a look at the docs for Timer class

    http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html << Updated Link

    This is the method I used from the timer class in the example below. So for your SOS you could quite possible add a forloop to run the sequence for so many seconds, and also control your LED light.

    public void schedule(TimerTask task,
                long delay,
                long period)
    

    //Usage of timer

    import java.util.*;

    public class TimerDemo {

    public static void main(String[] args) {
        // declare and create task
        TimerTask taskNew = new TimerTask() {
            // runs our task
            @Override
            public void run() {
                System.out.println("Timer running...");
    
            }
        };
        // declare and create timer
        Timer myTimer = new Timer();
    
    
        /* schedule the timer (task scheduled, delay in ms before task is
         * execution, period in ms between successive tasks executions
         */
        myTimer.schedule(taskNew, 500, 1000);
    
    }
    

    }

    I hope this gives a better example.