A proximity sensor in android:
![]() |
Proximity sensor |
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.
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.
SensorManager:
SensorManager class is used to access the sensor in 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.
MainActivity.Java
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; import android.widget.Toast; public class MainActivity 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.activity_main); ProximitySensor = (TextView)findViewById(R.id.proximitySensor); ProximityMax = (TextView)findViewById(R.id.proximityMax); ProximityReading = (TextView)findViewById(R.id.proximityReading); // Now in the SensorActivity we access the device sensor using SensorManager, an instance of this class is got by calling getSystemService(SENSOR_SERVICE) . We implement the class with theSensorEventListener interface to override its methods to do actions on sensor value change. 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])); } } }; } |
No comments:
Post a Comment