Coleman Tyler
Coleman Tyler

Reputation: 41

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

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

Upvotes: 4

Views: 9931

Answers (2)

Elio Lako
Elio Lako

Reputation: 1351

The code that i am using to turn the flashlight off is :

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

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

Upvotes: 1

Stephan Branczyk
Stephan Branczyk

Reputation: 9375

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.

Upvotes: 1

Related Questions