Domagoj Lipušić Domagoj Lipušić - 1 month ago 17
Android Question

SOS flashlight - How to

I made a flashlight app and now I want to add sos morse code flashing. This is my code for it but it doesen't work quite well, it blinks too fast so I need some kind of a delay, can anyone help?

public void sos(View v) {
String myString = "111000111";
Handler handler = new Handler();


for (int x = 0; x < myString.length(); x++) {
if (myString.charAt(x) == '1') {
handler.postDelayed(new Runnable() {
public void run() {
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
}
}, 300);


handler.postDelayed(new Runnable() {
public void run() {
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
}
}, 300);


handler.postDelayed(new Runnable() {
public void run() {
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
}
}, 300);

}

}
}

GC_ GC_
Answer

Note: I am not an app programmer, but here is a sample of Thread.sleep(). Good luck.

 for (int x = 0; x < myString.length(); x++) {
    if (myString.charAt(x) == '1') {                
          params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
          camera.setParameters(params);
          camera.startPreview();
    }     
    Thread.sleep(300);      


    params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);


}
Comments