Learn to create a Proximity Detector App on Android
Android devices have a lot of different sensors. Amongst them, most of them have a proximity detector sensor. It detects when your hand is close of the device. In this tutorial, you are going to create a Proximity Detector Application for Android by using the proximity detector sensor. Note that this tutorial is also available on Youtube :
First thing to do is to get the Sensor Manager service of your device. Then, you need to get the proximity detector sensor by passing the Sensor.TYPE_PROXIMITY constant to getDefaultSensor() method of the Sensor Manager got previously. To monitor the activity of the proximity detector sensor, you need to create a SensorEventListener object. In the onResume() method of the main activity, you will have to register this listener to the Sensor Manager.
Thus, you should have the following code :
package com.ssaurel.sensors; import android.graphics.Color; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private SensorManager sensorManager; private Sensor proximitySensor; private SensorEventListener proximitySensorListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); if (proximitySensor == null) { Toast.makeText(this, "Proximity sensor is not available !", Toast.LENGTH_LONG).show(); finish(); } proximitySensorListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent sensorEvent) { if (sensorEvent.values[0] < proximitySensor.getMaximumRange()) { getWindow().getDecorView().setBackgroundColor(Color.RED); } else { getWindow().getDecorView().setBackgroundColor(Color.GREEN); } } @Override public void onAccuracyChanged(Sensor sensor, int i) { } }; } @Override protected void onResume() { super.onResume(); sensorManager.registerListener(proximitySensorListener, proximitySensor, 2 * 1000 * 1000); } @Override protected void onPause() { super.onPause(); sensorManager.unregisterListener(proximitySensorListener); } }
In the onSensorChanged method of the SensorEventListener object, you just have to get the first value returned by the array contained in the SensorEvent object. If the value returned is inferior to the maximum range of the proximity sensor, you are going to set the background color to red. Otherwise, the background color will be set to green.
Like you can see, it is important to unregister the SensorEventListener object when the activity is paused to avoid to drain battery. Last step is to change your Android Manifest to tell that the application will use the proximity sensor :
<uses-feature android:name="android.hardware.sensor.proximity" android:required="true" />
Now, you can run your application and enjoy your proximity detector application in action :
Leave a Reply
You must be logged in to post a comment.