So, I made a torch app... The normal Torch function is fully working, no problems.
The following explains the below function that can cause possible ANRs Say, I want to send out an SOS message (morse code) through the blinking flash. (its 111-000-111) So it's On-On-On-Off-Off-Off- and repeat.
(Read Slowly) I "On" it for a little while, then a little flash, "On" again - This is to distinguish two consecutive "On"s ... That's how I get three distinct flashes.
Trouble is, for Time delay, I put the thread to sleep.(This method has worked the best for me)
Here's my sample function , it flashes out SOS: (I have a seperate button for SOS)
public void sos(View v) {
String myString = "111000111";
for (int x = 0; x < myString.length(); x++) {
if (myString.charAt(x) == '1') {
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
c.setParameters(p);
c.startPreview();
flag = true;
} else {
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
c.setParameters(p);
flag = false;
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
c.setParameters(p);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
c.setParameters(p);
}
}
The above flashes out SOS just once. But while this flashing and blinking is going on , my app is frozen up. The Blinking SOS is perfect. But it doesn't respond to any button clicks or even Back Button to stop it midway.
I reckon this is because the thread(UI) is sleeping hence it doesn't care about what the user is doing)... But the click surely is registered, and if according to android if it isn't looked after in the stipulated period of time (I think a few seconds) ... The app goes ANR.
So, I need brief pauses with intervals defined by me (As above I use 300 milliseconds). But I also want to know how to get the app to simultaneously respond to say Button Clicks, or Back Button Presses so as to avoid ANR's and give the user the ability to shut down the blinking at any time.
How to do this ?
EDIT Waza_Be's Solution:
Okay so here's my modified function according to Waza_Be. The problem seems to be, it does wait 300 milliseconds(in my case) ...But I have several postDelay functions - So I think it's actually lining up all the functions together, and the waiting period of the 300 milliseconds is somewhat overlapping. After this period ends, all the code runs together at once, making it quite pointless.
When I tried this, the Flash blinks rather swiftly, not with the subtle pauses and blinking effect I want.
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() {
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
c.setParameters(p);
c.startPreview();
}
}, 300);
} else {
handler.postDelayed(new Runnable() {
public void run() {
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
c.setParameters(p);
}
}, 300);
}
handler.postDelayed(new Runnable() {
public void run() {
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
c.setParameters(p);
}
}, 300);
}
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
c.setParameters(p);
}
}
I need separate pauses. Say after or before every TURN_ON or TURN_OFF for the Flash. In each loop, it should wait individually for each piece of code in the Handler Run().