Monday, September 19, 2011

Monitor the Proximity Sensor

Proximity sensor measures the distance that some object is from the device. It is often used to detect the presence of a person’s face next to the device.

Proximity Sensor

Some proximity sensors only support a binary near or far measurement. In this case, the sensor should report its maximum range value in the far state and a lesser value in the near state.

package com.exercise.AndroidProximitySensor;

import android.app.Activity;
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;

public class AndroidProximitySensorActivity extends Activity {
/** Called when the activity is first created. */

TextView ProximitySensor, ProximityMax, ProximityReading;

SensorManager mySensorManager;
Sensor myProximitySensor;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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) {
// TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub

if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){
ProximityReading.setText("Proximity Sensor Reading:"
+ String.valueOf(event.values[0]));
}
}
};
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/proximitySensor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/proximityMax"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/proximityReading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Download the files.

6 comments:

S. Morgan Biggs said...

Thanks! It's just what I have been looking for--a primer for the proximity sensor.

Mike said...

Nice Tutorial! Perfect for start playing with sensors! :) Just one question, do you know how we can unregister the SensorEventListener on this case? Regards

Erik said...

hello Mike,

to unregister the SensorEventListener, simple call unregisterListener().

Anonymous said...

Thank you for your fast response! :)

I was trying myProximitySensor.unregisterListener() instead of mySensorManager.. Worked like a charm! ;)

Anonymous said...

Sir, how to use this if we want to answer an incoming call by waving our our hand over the phone.

Unknown said...

sir how to us e this sensor to run in background so that when mobile is locked sensor is active/true ? What changes we have to do?