4

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

Saran
  • 3,845
  • 3
  • 37
  • 59
Coleman Tyler
  • 41
  • 1
  • 3

2 Answers2

1

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.

Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49
  • Yes, (isFlashOn) would be much more sufficient. What would you suggest i change onResume() to? and here is the link to my logcat : http://pastebin.com/sUS3tqDf – Coleman Tyler May 02 '14 at 21:46
  • Sorry, the logcat doesn't tell us what's wrong. At this point, I'd just set a breakpoint on hasFlash/isFlashOn, and go through it to see if the resulting value matches your expectation at each step. That will tell you what you need to write in onResume() – Stephan Branczyk May 03 '14 at 19:49
1

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;
    }
}
Elio Lako
  • 1,333
  • 2
  • 16
  • 26