How do i get torch in flashlight app to turn off?

Coleman Tyler picture Coleman Tyler · Apr 30, 2014 · Viewed 8k times · Source

I have been making a flashlight app for android and have ran into several problems and have been able to fix them thusfar. But now when I run the app it will load up on my HTC one and run fine until I press the flashlight power button the fourth time. I can press it once and it will turn on. I press it the second time and it turns off. I press it the third time and it turns back on. I press it the fourth and it won't turn off? Help?

public class MainActivity extends Activity {

  ImageButton powerButton;
  private Camera camera;
  private boolean isFlashOn;
  private boolean hasFlash;
  Parameters params;

  @TargetApi(Build.VERSION_CODES.ECLAIR)
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    powerButton = (ImageButton) findViewById(R.id.power_button);

    hasFlash = getApplicationContext().getPackageManager()
    .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

    if (!hasFlash) {
      AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
      .create();
      alert.setTitle("Notice");
      alert.setMessage("I'm sorry, your device doesn't support flashlight!");
      alert.setButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          finish();
        }
      });
      alert.show();
      return;
    }

    getCamera();

    toggleButtonImage();



    powerButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        if (isFlashOn) {
          turnOffFlash();
        } else {
          turnOnFlash();
        }
      }
    });
  }


  private void getCamera() {
    if (camera == null) {
      try {
        camera = Camera.open();
        params = camera.getParameters();
      } catch (RuntimeException e) {
        Log.e("Failed to open camera. Alert: ", e.getMessage());
      }
    }
  }


  public void turnOnFlash() {
    if (!isFlashOn) {
      if (camera == null || params == null) {
        return;
      }

      params = camera.getParameters();
      params.setFlashMode(Parameters.FLASH_MODE_TORCH);
      camera.setParameters(params);
      camera.startPreview();
      isFlashOn = true;

      toggleButtonImage();
    }

  }

  private void turnOffFlash() {
    if (isFlashOn) {
      if (camera == null || params == null) {
        return;
      }

      params = camera.getParameters();
      params.setFlashMode(Parameters.FLASH_MODE_OFF);
      camera.setParameters(params);
      camera.stopPreview();
      isFlashOn = false;

      toggleButtonImage();
    }
  }

  private void toggleButtonImage(){
    if(isFlashOn){
      powerButton.setImageResource(R.drawable.flashlight_on);
    }else{
      powerButton.setImageResource(R.drawable.flashlight_off);
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
  }

  @Override
  protected void onPause() {
    super.onPause();
    turnOffFlash();
  }

  @Override
  protected void onRestart() {
    super.onRestart();
  }

  @Override
  protected void onResume() {
    super.onResume();
    if(hasFlash)
      turnOffFlash();
  }

  @Override
  protected void onStart() {
    super.onStart();

    getCamera();
  }

  @Override
  protected void onStop() {
    super.onStop();

    if (camera != null) {
      camera.release();
      camera = null;
    }
  }

' Manifest - http://pastebin.com/Tw6LTemP ' Activity_main.xml - http://pastebin.com/Tga1agN7

Answer

Stephan Branczyk picture Stephan Branczyk · Apr 30, 2014

As to the symptom you're describing, it's probably the SurfaceView crashing. Please post your error log from logcat.

Also, your code inside onResume() does not make sense to me (although, that's not the direct cause of your problem, for the direct cause of your problem, please again see your logcat):

@Override
protected void onResume() {
super.onResume();
if(hasFlash)
    turnOffFlash();
}

I think you meant to say if (isFlashOn) turnOffFlash(); but even if you correct it to that, that is still wrong.