0

I'm trying to create an app that sends out a morse code by using the flash of my cell phone on android. With a timer I was able turn the flash on and off with certain intervals, but the user is unable to cancel the operation. Is there a way to implement this functionality? What class/function should I use to implement the waits between the led on/led offs?

Thanks in advance, Cédric

this is my current code to make the flash work

public void sendSOS()
    {


        t=new Timer();

        //kort kort kort
        t.schedule(new ShortFlash(),0);
        t.schedule(new ShortFlash(),700);
        t.schedule(new ShortFlash(),1400);

        //lang lang lang
        t.schedule(new LongFlash(),2400);
        t.schedule(new LongFlash(),3600);
        t.schedule(new LongFlash(),4800);

        //kort kort kort
        t.schedule(new ShortFlash(),6200);
        t.schedule(new ShortFlash(),6900);
        t.schedule(new ShortFlash(),7600);

    }


    class ShortFlash extends TimerTask
    {
        public void run()
        {
            cam=Camera.open();
            Camera.Parameters params = cam.getParameters();
            params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
            cam.setParameters(params);
            cam.startPreview();
            cam.autoFocus(new Camera.AutoFocusCallback() {
                public void onAutoFocus(boolean success, Camera camera) {
                }
            });

            android.os.SystemClock.sleep(500);
            cam.stopPreview();
            cam.release();
        }
    }

    class LongFlash extends TimerTask
    {
        public void run()
        {
            cam=Camera.open();
            Camera.Parameters params = cam.getParameters();
            params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
            cam.setParameters(params);
            cam.startPreview();
            cam.autoFocus(new Camera.AutoFocusCallback() {
                public void onAutoFocus(boolean success, Camera camera) {
                }
            });

            android.os.SystemClock.sleep(1000);
            cam.stopPreview();
            cam.release();
        }
    }
Cédric
  • 419
  • 7
  • 17
  • Thread.currentThread().sleep(millis); will cause the thread to sleep for a number of milliseconds. – Chris Hinshaw Oct 03 '14 at 21:44
  • @ChrisHinshaw that will work definetley, but is the worst way to make the operation cancelable and pausable. – Gumbo Oct 03 '14 at 21:45
  • Please give us some code, so we can find the solution that fits best for your implementation. Also, we don't have to think that much ourselves that way :D – Gumbo Oct 03 '14 at 21:47
  • In that case you handle the puase and cancel and call Thread.interrupt(). This should stop the sleep and continue on. You may take a look at this http://stackoverflow.com/questions/5915156/how-can-i-kill-a-thread-without-using-stop – Chris Hinshaw Oct 03 '14 at 21:50
  • 2
    This might help [Morse Code Question][1] [1]: http://stackoverflow.com/questions/18185384/blinking-flash-according-to-morse-code-android-how-to-avoid-anrs-due-to-sleepi – Samer Jalaleddine Oct 03 '14 at 22:19

0 Answers0