Proximity sensor android example[Updated]

What is Proximity sensor android?

A proximity sensor in Android is used to detect the presence of the object without touching the device.

Proximity sensor emits an electromagnetic field and looks for the changes in the return signal. In real-time, proximity sensor used in many ways in the Android mobile device.

proximity sensor on android phone
proximity sensor on android phone

Android proximity sensor uses in mobile

One of the major use of the proximity sensor in Android is to switch off the display and lock the mobile screen when the user attending the call and getting the phone close to the ears.

Then it will switch on the screen and unlock the device when the user takes off the phone from the ears. The main use of this functionality is to avoid the key press or other events when speaking on the phone in ears.

How to disable/enable proximity sensor android?

  • Turn on your phone, go to “Settings“, then “System Apps“.
  • Scroll down the applications list to “Call settings“.
  • Tap “Incoming call settings“.
  • Disable/Enable a proximity sensor android settings. The procedure and tools may vary depending on your phone model and Android OS version.

Important components of proximity sensor android

SensorManager

SensorManager class is used to access the sensor on the android device. You can get the instance of the sensor manager in Android by calling the getSystemService method with the Context.SENSOR_SERVICE.

SensorManager  mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

Sensor

get the Sensor from the  SensorManager. here I am getting the Sensor.TYPE_PROXIMITY from the sensorManager.

Sensor myProximitySensor = mySensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

And the check the proximity sensor is present in the device or not. If the sensor is null. the device doesn’t have the proximity sensor.

Done with the explanation on android proximity sensor. Let move to proximity sensor android example part.

proximity sensor android app example

Create the Sensor class object.

Sensor myProximitySensor = mySensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

Get The SensorManager,

SensorManager mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

Check the Proximity Sensor Available on the device or not.

Sensor myProximitySensor = mySensorManager.getDefaultSensor(
 Sensor.TYPE_PROXIMITY);
if (myProximitySensor == null){
 ProximitySensor.setText("No Proximity Sensor!");
}else{
 ProximitySensor.setText("Proximity Sensor Available");
}

Implement SensorEventListener() to get Proximity Sensor values updates,

SensorEventListener proximitySensorEventListener = new SensorEventListener(){
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
    @Override
    public void onSensorChanged(SensorEvent event) {
        if(event.sensor.getType()==Sensor.TYPE_PROXIMITY)
        {
            ProximityReading.setText("Proximity Sensor Reading:"
                    + String.valueOf(event.values[0]));
        }
    }
};

Apply SensorEventListener() to Proximity Sensor,

mySensorManager.registerListener(proximitySensorEventListener,myProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);

Final MainActivity.java

public class MainActivity extends AppCompatActivity {
    TextView ProximitySensor, ProximityMax, ProximityReading;
    SensorManager mySensorManager;
    Sensor myProximitySensor;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ProximitySensor = (TextView)findViewById(R.id.proximitySensor);
        ProximityMax = (TextView)findViewById(R.id.proximityMax);
        ProximityReading = (TextView)findViewById(R.id.proximityReading);
        mySensorManager = (SensorManager)getSystemService(
                Context.SENSOR_SERVICE);
        myProximitySensor = mySensorManager.getDefaultSensor(
                Sensor.TYPE_PROXIMITY);
        if (myProximitySensor == null){
            ProximitySensor.setText("No Proximity Sensor!");
        }else{
            ProximitySensor.setText(myProximitySensor.getName());
            ProximityMax.setText("Maximum Range: "
                    + String.valueOf(myProximitySensor.getMaximumRange()));
            mySensorManager.registerListener(proximitySensorEventListener,
                    myProximitySensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        }
    }
    SensorEventListener proximitySensorEventListener = new SensorEventListener(){
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
        @Override
        public void onSensorChanged(SensorEvent event) {
            if(event.sensor.getType()==Sensor.TYPE_PROXIMITY)
            {
                ProximityReading.setText("Proximity Sensor Reading:"
                        + String.valueOf(event.values[0]));
            }
        }
    };
}

Screenshot

Proximity Sensor Android

I hope you like this android proximity sensor app example. Please share your feedback on comments section. Thank you.


Related Post

Get direction between two points google maps android

how to load image from sdcard in android

custom toast android example


Download Proximity sensor android example

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top