On Touchlistener doesn´t work to turn off light on Android

186 views Asked by At

I tried to use an onTouch and OnClicklistener to turn the light on and off. It turns on the light, but doesn't turn it off again. I don't know what I'm doing wrong, because it turns on.

Here is my code:

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.List;


public class MainActivity extends ActionBarActivity {
    public Camera camera;
    private Camera.Parameters params;
    ImageButton flashlightSwitchImg;
    private boolean isFlashlightOn;
    private boolean isFlashLightOff;
    TextView textViewbutton;
    private boolean hasCam;
    private SurfaceHolder mHolder;


    public boolean hasFlash() {
        if (camera == null) {
            return false;
        }

        Camera.Parameters parameters = camera.getParameters();

        if (parameters.getFlashMode() == null) {
            return false;
        }

        List<String> supportedFlashModes = parameters.getSupportedFlashModes();
        if (supportedFlashModes == null || supportedFlashModes.isEmpty() || supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) {
            return false;
        }

        return true;

    }

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

        flashlightSwitchImg = (ImageButton) findViewById(R.id.imageButton);
        textViewbutton = (TextView) findViewById(R.id.textView2);


        try {
            //Log.d("TORCH", "Check cam");
            // Get CAM reference
            camera = Camera.open();
            params = camera.getParameters();
            camera.startPreview();
            hasCam = true;
            //Log.d("TORCH", "HAS CAM ["+hasCam+"]");
        } catch (Throwable t) {
            t.printStackTrace();
        }

        if (hasFlash()) {

        } else {

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

            alertDialog.setCancelable(false);

            // Setting Dialog Title
            alertDialog.setTitle("Flashlight not available...");

            // Setting Dialog Message
            alertDialog.setMessage("Your device is not compatible with this app or doesn't have a flashlight. DO YOU WANT TO USE YOUR SCREEN AS A TORCH INSTEAD?");


            // Setting Positive "Yes" Button
            alertDialog.setPositiveButton("Yes, of course", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    FrameLayout legacy = (FrameLayout) findViewById(R.id.legacy);
                    // Write your code here to invoke YES event
                    legacy.setVisibility(View.VISIBLE);
                    WindowManager.LayoutParams layout = getWindow().getAttributes();
                    layout.screenBrightness = 1F;
                    getWindow().setAttributes(layout);
                }
            });

            // Setting Negative "NO" Button
            alertDialog.setNegativeButton("No, thanks", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to invoke NO event
                    System.exit(0);
                }
            });

            // Showing Alert Message
            alertDialog.show();
        }

        flashlightSwitchImg.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (isFlashlightOn) {
                    turnOff();
                } else {
                    turnOn();
                }

                return false;
            }

        });
    }



    public void turnOn() {
        params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        textViewbutton.setText(R.string.light_turn_off);

    }


    public void turnOff() {
        params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        camera.setParameters(params);
        camera.stopPreview();
        textViewbutton.setText(R.string.lo);
    }


}
1

There are 1 answers

4
GMYeti On

picking at the little things looks as thought you never change the value of "isFlashlightOn" or "isFlashlightOff". you are using "isFlashlightOn" in you if statement.

flashlightSwitchImg.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (isFlashlightOn) {
                    turnOff();
                } else {
                    turnOn();
                }

                return false;
            }

        });

So i recomend removing "isFlashlightOff", and changing the "IsFlashlightOn" Value here.

public void turnOn() {
    params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    camera.setParameters(params);
    camera.startPreview();
    textViewbutton.setText(R.string.light_turn_off);

}


public void turnOff() {
    params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.stopPreview();
    textViewbutton.setText(R.string.lo);
}

like so:

public void turnOn() {
    params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    camera.setParameters(params);
    camera.startPreview();
    isFlashlightOn = True;
    textViewbutton.setText(R.string.light_turn_off);

}


public void turnOff() {
    params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.stopPreview();
    isFlashlightOn = False;
    textViewbutton.setText(R.string.lo);
}

By the way. it still runs without crashing but only turns on because the value defaults to false.