Я создал следующий стробоскоп. Я хочу, чтобы он начинался с нажатия кнопки и останавливался, если снова нажать. Прямо сейчас, когда он открыт, и я нажимаю, он закрывается, но при повторном нажатии ничего не происходит!Стробоскоп работает отлично, но не работает очень хорошо при нажатии
Без прослушивания кликов он начинается с начала приложения, но отлично работает, но остановить его невозможно.
public class Small extends Activity {
private MMAdView adViewFromXml;
RefreshHandler handler;
ImageButton knob;
int n=100000;
Camera mCamera;
Parameters params;
int delay = 400; // in ms
public boolean on;
public boolean works;
Thread logotimer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_small_button);
knob = (ImageButton) findViewById(R.id.pic);
strobe();
knob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View iv) {
if(works == true){
logotimer.interrupt();
}else if(works != true)
{
strobe();
}
}
});
});
}
/** Turn the devices FlashLight on */
public void turnOn() {
if (mCamera != null) {
// Turn on LED
params = mCamera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
mCamera.startPreview();
on = true;
}
}
/** Turn the devices FlashLight off */
public void turnOff() {
// Turn off flashlight
if (mCamera != null) {
params = mCamera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.stopPreview();
}
on = false;
}
/** Toggle the flashlight on/off status */
/*public void toggleFlashLight() {
if (!on) { // Off, turn it on
turnOn();
} else { // On, turn it off
turnOff();
}
}*/
private void strobe(){
Thread logotimer = new Thread() {
public void run() {
try {
// Switch on the cam for app's life
if (mCamera == null) {
// Turn on Cam
try{
mCamera = Camera.open();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
try {
mCamera.setPreviewDisplay(null);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
int logotimer = 0;
while(!interrupted() && logotimer <5000) {
logotimer = logotimer ++;
works = true;
if (!on) { // Off, turn it on
turnOn();
} else if(on == true) { // On, turn it off
turnOff();
}
sleep(delay);
}
if (mCamera == null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
} catch (InterruptedException e){
e.printStackTrace();
}
}
};logotimer.start();
}
}
LogCat:
01-08 15:17:33.807: W/System.err(28814): java.lang.InterruptedException
01-08 15:17:33.808: W/System.err(28814): at java.lang.VMThread.sleep(Native Method)
01-08 15:17:33.808: W/System.err(28814): at java.lang.Thread.sleep(Thread.java:1013)
01-08 15:17:33.808: W/System.err(28814): at java.lang.Thread.sleep(Thread.java:995)
01-08 15:17:33.809: W/System.err(28814): at com.light.oid.Small$4.run(Small.java:163)
при нажатии на ручку, вы отпустите камеру, но не остановить цикл строба. – njzk2
вам нужно остановить цикл в методе onclick и оставить камеру в потоке. – njzk2
@ njzk2 Не могли бы вы уточнить, возможно, покажите мне код –