简体   繁体   English

频闪灯工作正常,但单击时效果不佳

[英]Strobe light is working fine but doesn't work very well on click

I have created the following strobe light. 我创建了以下频闪灯。 I want it to start on the press of a button and to stop if pressed again. 我希望它在按下按钮时开始,如果再次按下则停止。 Right now when it is open and I click, it closes but, on clicking again nothing happens! 现在,当它打开并单击时,它会关闭,但是再次单击时,什么也不会发生!

Without the click listener, it starts on the start of the app but works great, but there is no way to stop it. 没有点击侦听器,它会在应用程序启动时启动,但效果很好,但是无法停止它。

    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)

Add strobe() call inside onClick: 在onClick内添加strobe()调用:

@Override
public void onClick(View iv) {
    if(works) {
        logotimer.interrupt();

    } else {
        //mCamera = Camera.open(); //remove this
        //and add strobe()
        strobe(); 
    }
}

Also, you have global Thread logotimer, but in your strobe() method, you are creating a local variable with the same name, so when clicking button, logotimer is null, because the local shadows the global. 另外,您具有全局Thread logotimer,但是在strobe()方法中,您将创建一个具有相同名称的局部变量,因此,在单击按钮时,logotimer为null,因为局部阴影覆盖了全局变量。

In strobe(), change: 在strobe()中,更改:

 private void strobe(){
     Thread logotimer = new Thread() {
         public void run() {

to: 至:

 private void strobe(){
     logotimer = new Thread() {
         public void run() {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM