我创造了以下闪光灯。我希望它按下按钮开始,如果再次按下则停止。现在当它打开并且我点击时,它会关闭但是再次点击时没有任何反应!
没有点击监听器,它会在应用程序启动时启动,但效果很好,但无法阻止它。
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)
答案 0 :(得分:0)
在onClick中添加strobe()调用:
@Override
public void onClick(View iv) {
if(works) {
logotimer.interrupt();
} else {
//mCamera = Camera.open(); //remove this
//and add strobe()
strobe();
}
}
此外,您有全局Thread logotimer,但在strobe()方法中,您正在创建一个具有相同名称的局部变量,因此当单击按钮时,logotimer为null,因为本地阴影全局。
在strobe()中,更改:
private void strobe(){
Thread logotimer = new Thread() {
public void run() {
为:
private void strobe(){
logotimer = new Thread() {
public void run() {