[英]Flashlight blink on button click in android
I am developing a flashlight application in which I am trying to add blink functionality on button click.我正在开发一个手电筒应用程序,我试图在其中添加按钮单击时的闪烁功能。 The code that i found for this is:
我为此找到的代码是:
String myString = "0101010101";
long blinkDelay 50; //Delay in ms
for (int i = 0; i < myString.length(); i++) {
if (myString.charAt(i) == '0') {
params.setFlashMode(Parameters.FLASH_MODE_ON);
} else {
params.setFlashMode(Parameters.FLASH_MODE_OFF);
}
try {
Thread.sleep(blinkDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
But this code turns off flashlight after few blinks.但是此代码在几次闪烁后关闭手电筒。 How can i start flashlight blink on button click and stop it unless I click it again?
如何在单击按钮时启动手电筒闪烁并停止它,除非我再次单击它? Any Help?
任何帮助?
It's controlled by your string length so the for loop will break after the count of i becomes greater than last index.它由您的字符串长度控制,因此在 i 的计数大于最后一个索引后 for 循环将中断。 Use a while loop if you want to blink the flash continuously.
如果您想连续闪烁闪光灯,请使用 while 循环。 You can use one boolean variable to switch between on off.
您可以使用一个布尔变量在开关之间切换。 And a boolean in while condition to break the loop when button is clicked
当单击按钮时,while 条件中的布尔值会中断循环
Hope it helps.希望它有帮助。
You will have to use a thread to prevent UI freeze.您将不得不使用线程来防止 UI 冻结。 Thread will contain a while loop which will allow it to blink continuously.
线程将包含一个 while 循环,它将允许它连续闪烁。
while (shouldGlow ) {
flashLight();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//use boolean varibale to stop the loop //使用布尔变量来停止循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.