How to get current value of Android's proximity sensor? - Stack Overflow
Description: Is it possible to get the current value of the Proximity Sensor in Android? I know that I can use SensorManager and Sensor and register a state changed listener, but I have no need to be notifie...

# How to get current value of Android's proximity sensor?

Ask Question
Asked 8 years, 10 months ago
Modified 7 years, 7 months ago
Viewed 8k times
Part of Mobile Development Collective
7

Is it possible to get the current value of the Proximity Sensor in Android?

I know that I can use SensorManager and Sensor and register a state changed listener, but I have no need to be notified of every state change, so it would be highly inefficient since this code is being run in a service. Also, my code won't know the value until a state change has occurred (what if the value hasn't changed...how do I know what it is? Instead, rather than registering a listener, I just want to say:

    proximitySensor.getCurrentDistance();

Is this possible?

Thanks

- android
- android-sensors
- sensormanager
- android-developer-api
- proximitysensor

edited Feb 10, 2016 at 1:43
Nullqwerty
asked Feb 9, 2016 at 21:56
Nullqwerty Nullqwerty
1,16811 gold badge2121 silver badges3838 bronze badges

Add a comment |

## 2 Answers 2

Sorted by: Reset to default
4

After looking through the documentation, it looks like you can get the distance in centimeters by subscribing to the SensorEvent and looking at the data being passed back.

There is a good example of starting to use the Proximity sensor here: Android Proximity Sensor Example

After reading a bit further into the Android docs, it looks like the array `values[0]` returns a value in centimeters. Note, look at the docs, some sensors only return binary values, meaning that the device is either near or far.

answered Feb 9, 2016 at 22:31
user3924722 user3924722
4922 bronze badges
4

- 1
  Yes, but I'm asking how to do it without using those listeners.
  – Nullqwerty
  Commented Feb 10, 2016 at 1:41
- From my post: "I know that I can use SensorManager and Sensor and register a state changed listener, but I have no need to be notified of every state change, so it would be highly inefficient since this code is being run in a service. Also, my code won't know the value until a state change has occurred (what if the value hasn't changed...how do I know what it is?"
  – Nullqwerty
  Commented Feb 10, 2016 at 1:41
- 1
  I apologize, I was multi-tasking while answering, my fault. I don't know that there is really any other way than to listen for the changes.
  – user3924722
  Commented Feb 10, 2016 at 19:41
- There appears to be a behavior difference between real device and an emulator: On real device, as soon as I register the listener, the `onSensorChanged` callback is called.
  – Martin Ptáček
  Commented Jul 26, 2023 at 17:53

Add a comment |
2

To get access the device sensor using SensorManager, you have to call getSystemService(SENSOR_SERVICE) . Here is the example:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".SensorActivity" >

    <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/far" />

    </RelativeLayout>

Here is the java class:

    import android.app.Activity;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.widget.ImageView;

    public class SensorActivity extends Activity implements SensorEventListener {
     private SensorManager mSensorManager;
     private Sensor mSensor;
     ImageView iv;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.sensor_screen);
      mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
      mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
      iv = (ImageView) findViewById(R.id.imageView1);
     }

     protected void onResume() {
      super.onResume();
      mSensorManager.registerListener(this, mSensor,
        SensorManager.SENSOR_DELAY_NORMAL);
     }

     protected void onPause() {
      super.onPause();
      mSensorManager.unregisterListener(this);
     }

     public void onAccuracyChanged(Sensor sensor, int accuracy) {
     }

     public void onSensorChanged(SensorEvent event) {
      if (event.values[0] == 0) {
       iv.setImageResource(R.drawable.near);
      } else {
       iv.setImageResource(R.drawable.far);
      }
     }
    }

answered May 11, 2017 at 6:05
Md. Sajedul Karim Md. Sajedul Karim
7,02544 gold badges6464 silver badges8989 bronze badges

Add a comment |

## Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

- Please be sure to answer the question. Provide details and share your research!

But avoid …

- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

### Sign up or log in

Sign up using Google
Sign up using Email and Password

### Post as a guest

Name
Email

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

##
Not the answer you're looking for? Browse other questions tagged

- android
- android-sensors
- sensormanager
- android-developer-api
- proximitysensor

or ask your own question.

default