Every android mobile is shipped with
sensors to measure various environmental conditions. In this example
we will see how to use the proximity sensors in an android
application. Proximity sensors would be useful to reveal the nearness
of an object to the phone. We might often experience that our phone
screen would turn off when we bring the phone to our ears when we are
in a call and the screen will turn on when we take it back. This is
because the proximity sensor recognizes the object near the phone.
In this example we will change the
image in the ImageView w.r.t the nearness of any object to the phone.
Check the sensor functionality by covering the sensor region (mostly
the top left of the phone) with your hand .
So lets start
1.Create
a new project File
->New -> Project ->Android ->Android Application Project.
While creating the new project, name the activity as
SensorActivity(SensorActivity.java)
and layout as sensor_screen.xml.
2.Now
let us design the UI for the SensorActivity i.e sensor_screen.xml
with an ImageView.Have two images in drawable folder to differentiate
near and far activity.
sensor_screen.xml:
<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>
3.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 the SensorEventListener interface to override its methods to do actions on sensor value change.
SensorActivity.java:
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); } } }
4.Run the project by rightclicking project Run as → android project.
Output:
The
output of this example would be similar to the one as follows: