title: Android Flashlight Won't Turn Off
body: <p>I am making an app that uses touchless gestures to turn off and on the flashilight in my Android app. It turns on just fine, but I can't get it to turn back off. Thanks in advance for any help. </p>
  
  <p>Here is my MainActivity.java</p>
  
  <pre><code>      package com.proximitysensorapp.primo.app;
  
      import android.hardware.Sensor;
      import android.hardware.SensorEvent;
      import android.hardware.SensorEventListener;
      import android.hardware.SensorManager;
      import android.os.Bundle;
      import android.app.Activity;
      import android.content.Intent;
      import android.widget.TextView;
      import java.io.IOException;
      import android.content.pm.PackageManager;
      import android.hardware.Camera;
      import android.hardware.Camera.Parameters;
      import android.util.Log;
      import android.view.SurfaceHolder;
      import android.view.SurfaceHolder.Callback;
      import android.view.SurfaceView;
      import android.view.View;
      public class MainActivity extends Activity implements SensorEventListener, Callback 
     {
     SensorManager sm;
     Sensor s;
     TextView et1;
     Intent Activitynew;
     Camera camera;
     SurfaceView surfaceView;
     SurfaceHolder surfaceHolder; 
     public void TurnOn(View view) 
     {
     if (FlashAvailable() &amp;&amp; camera != null) 
     {
     Parameters p = camera.getParameters();
     p.setFlashMode(Parameters.FLASH_MODE_TORCH);
     camera.setParameters(p);
     //camera.startPreview();
     try 
     {
     camera.setPreviewDisplay(surfaceHolder);
     } 
     catch (IOException e) {
     e.printStackTrace();
     }            
     }
     }
     public void TurnOff(View view) {
     if (FlashAvailable() &amp;&amp; camera != null) {
     Parameters p = camera.getParameters();
     p.setFlashMode(Parameters.FLASH_MODE_OFF);
     camera.setParameters(p);
     //camera.stopPreview();          
     }
     }
     @SuppressWarnings("deprecation")
     @Override
     protected void onCreate(Bundle savedInstanceState) 
     {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     sm = (SensorManager) getSystemService(SENSOR_SERVICE);
     s = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
     sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
     Activitynew = new Intent(MainActivity.this, Activity2.class);
     surfaceView = (SurfaceView) this.findViewById(R.id.surface1);
     surfaceHolder = surfaceView.getHolder();
     surfaceHolder.addCallback(this);
     surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
     }
     @Override
     public void onResume() 
     {
    super.onResume();
    try {
    camera = Camera.open();     
    } catch (Exception e) {
    //---exception handling here---
    Log.d("Flashlight", e.toString());
    }
    }
  
    @Override
    public void onPause() {
    super.onPause();
    TurnOff(null);
    camera.release();
    }
  
  
    private Boolean FlashAvailable() {
    return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }
  
  
    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) 
    {
  
    }
    @Override
    public void onSensorChanged(SensorEvent event) 
    {
    if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) 
    {
    int temp = (int) event.values[0];
  
    if (temp==1)
    {
    TurnOff(null);
    }
    else if (temp==0)
    {
    TurnOn(null);
    }
    }
    }
  
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
  
    }
  
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) {
    // TODO Auto-generated method stub
  
    }
  
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
  
    }
    }
  </code></pre>