简体   繁体   中英

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:

@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.

In strobe(), change:

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

to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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