How to use proximity device sensors in Android

In this android programming source code example, we are going to use proximity device sensors In Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to use proximity device sensors In Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DeviceFeatures.DeviceFeaturesActivity15">

    <TextView
        android:id="@+id/proximityTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Proximity Text"
        android:gravity="center"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import com.bluapp.androidview2.R;

public class DeviceFeaturesActivity15 extends AppCompatActivity {
    private TextView proximityTxt;
    private SensorManager sensorManager;
    private Sensor sensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_features15);
        proximityTxt = (TextView) findViewById(R.id.proximityTxt);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
        if (sensor == null) {
            proximityTxt.setText("No Proximity Sensor!");
        } else {
            sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    private SensorEventListener sensorEventListener = new SensorEventListener() {
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onSensorChanged(SensorEvent event) {
            // TODO Auto-generated method stub
            if (event.sensor.getType() < sensor.getMaximumRange()) {
                if (event.values[0] == 0) {
                    proximityTxt.setText("Proximity Is Close");
                } else {
                    proximityTxt.setText("Proximity Is Far");
                }
            }
        }
    };
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment