How to make camera take picture when proximity sensor is activated?
<p><strong>I'm interested in knowing how to activate the camera the i have in my program when the proximity sensor is triggered or activated.</strong> I already made a sensor and a camera button that launches the camera on the phone but I can't find a way to make it so the sensor when triggered it launches the camera and takes the picture. By the way I'm using Android Studio to make this.</p>

<p><strong>Here is my code from Main Activity:</strong></p>

<pre><code>package com.adalhub.adalhub.sensor;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
import java.io.File;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

TextView proxText;
SensorManager sm;
Sensor proxSensor;


private static String logtag = "CameraApp8";
private static int TAKE_PICTURE = -1;
private Uri imageUri;


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


    Button cameraButton = (Button)findViewById(R.id.button_camera);
    cameraButton.setOnClickListener(cameraListener);
    // calls button of camera, when button is clicked the camera decides to got to cameraListener










    sm= (SensorManager) getSystemService(SENSOR_SERVICE);
    proxSensor=sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    proxText= (TextView)findViewById(R.id.proximityTextView);
    // this stuff literaly only makes proximity sensor

    sm.registerListener(this, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
    // this is just the type of prox sensor






}









    // when click button this is triggered
private OnClickListener cameraListener = new OnClickListener()
{
    @Override
    public void onClick(View v) {
        // makes it go into camera mode
        takePhoto(v);
    }

};



private void takePhoto(View v){
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo= new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture.jpg");
    imageUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode,Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);

    if(resultCode == Activity.RESULT_OK){
        Uri selectedImage= imageUri;
        getContentResolver().notifyChange(selectedImage, null);

        ImageView imageView = (ImageView)findViewById(R.id.image_camera);
        ContentResolver cr = getContentResolver();
        Bitmap bitmap;

        try{
            bitmap = MediaStore.Images.Media.getBitmap(cr, selectedImage);
            imageView.setImageBitmap(bitmap);
            Toast.makeText(MainActivity.this, selectedImage.toString(), Toast.LENGTH_LONG).show();

        }catch(Exception e){
            Log.e(logtag, e.toString());
        }
    }
}
















@Override
public void onSensorChanged(SensorEvent event) {

    proxText.setText(String.valueOf(event.values[0]));
    //SET AUTOMATIC PHOTO HERE


}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {



}
</code></pre>

<p>}</p>