Sensor Exmaple in Android(Accelerometer and Proximitry Sensor)
This is an example that will show how to use sensors in android. This example includes two sensors:
1) Accelerometer Sensor
2) Proximity Sensor
This is an example that will show you how can you move any object(ex:- Textview) in android using an accelerometer sensor. And also use proximity sensors to generate toast. You can do anything with this sensor that you want for your application.
Steps:-
1) First you need to implement SensorEventListener and overrides its method.
2) Create Object of SensorManager class.
3) Register all sensor listeners with the sensor manager object.
4) The unregistered sensor when activity destroyed or pause.
Note:- Don't forget to unregistered your sensor once you are done with the sensor.
Note:- In proximity sensor values are different as per the mobile company. In this example, I am using moto g (1st gen) the value of the proximity sensor are 3.0 for near and 100.0 for far.
For most of the other devices, values are 0 for near and 1 for far.
MainActivity.java
public class MainActivity extends AppCompatActivity implements SensorEventListener{
private SensorManager sensorManager;
private TextView tv;
int height; // For getting height of the device
int width; // For getting width of the device
private float textViewX,textViewY; // For the movement of textView on the screen
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);
tv= (TextView) findViewById(R.id.content_main_text);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
}
@Override
public void onSensorChanged(SensorEvent event) {
// call if the accelerometer sensor is used
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
//Put your logic here
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
textViewX=(width/2)+((width/20)*x)-tv.getWidth()/2;
textViewY=(height/2)+((height/20)*y)-tv.getHeight()/2;
if(textViewX<width-tv.getWidth() && textViewX>0)
tv.setX(textViewX);
if(textViewY<height-tv.getHeight()*2 && textViewY>0)
tv.setY(textViewY);
}
//call if the proximitry sensor is used
if(event.senso r.getType()==Sensor.TYPE_PROXIMITY){
//Put your logic here
if (event.values[0] == 3) {
Toast.makeText(getApplicationContext(), "Near",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Far", Toast.LENGTH_SHORT).show();
}
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy){}
@Override
protected void onResume() {
super.onResume();
//register your sensor here
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
//Unregister your sensor
sensorManager.unregisterListener(this);
}}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:gravity="center"
android:id="@+id/content_main_text"
android:textSize="20sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Output
1) Accelerometer Sensor
2) Proximity Sensor
This is an example that will show you how can you move any object(ex:- Textview) in android using an accelerometer sensor. And also use proximity sensors to generate toast. You can do anything with this sensor that you want for your application.
Steps:-
1) First you need to implement SensorEventListener and overrides its method.
2) Create Object of SensorManager class.
3) Register all sensor listeners with the sensor manager object.
4) The unregistered sensor when activity destroyed or pause.
Note:- Don't forget to unregistered your sensor once you are done with the sensor.
Note:- In proximity sensor values are different as per the mobile company. In this example, I am using moto g (1st gen) the value of the proximity sensor are 3.0 for near and 100.0 for far.
For most of the other devices, values are 0 for near and 1 for far.
MainActivity.java
public class MainActivity extends AppCompatActivity implements SensorEventListener{
private SensorManager sensorManager;
private TextView tv;
int height; // For getting height of the device
int width; // For getting width of the device
private float textViewX,textViewY; // For the movement of textView on the screen
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);
tv= (TextView) findViewById(R.id.content_main_text);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
}
@Override
public void onSensorChanged(SensorEvent event) {
// call if the accelerometer sensor is used
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
//Put your logic here
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
textViewX=(width/2)+((width/20)*x)-tv.getWidth()/2;
textViewY=(height/2)+((height/20)*y)-tv.getHeight()/2;
if(textViewX<width-tv.getWidth() && textViewX>0)
tv.setX(textViewX);
if(textViewY<height-tv.getHeight()*2 && textViewY>0)
tv.setY(textViewY);
}
//call if the proximitry sensor is used
if(event.senso r.getType()==Sensor.TYPE_PROXIMITY){
//Put your logic here
if (event.values[0] == 3) {
Toast.makeText(getApplicationContext(), "Near",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Far", Toast.LENGTH_SHORT).show();
}
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy){}
@Override
protected void onResume() {
super.onResume();
//register your sensor here
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
//Unregister your sensor
sensorManager.unregisterListener(this);
}}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:gravity="center"
android:id="@+id/content_main_text"
android:textSize="20sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Output
