The document discusses Android sensors and how to access sensor data in Android applications. It describes the three categories of sensors - motion, position, and environmental sensors. It explains how to get the SensorManager instance and use it to access specific sensors. The Sensor and SensorEvent classes provide information about individual sensors. The SensorEventListener interface defines callback methods to receive sensor data updates. The document provides an example Android app that uses the accelerometer sensor to change the view color when the device is shaken.
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
Android Sensors - Guide
1. Android – Sensors
Most of the android devices have built-in sensors that measure motion, orientation, and
various environmental conditions.
The android platform supports three broad categories of sensors.
1) Motion Sensors
These are used to measure acceleration forces and rotational forces along with three
axes.
2) Position Sensors
These are used to measure the physical position of device.
3) Environmental Sensors
These are used to measure the environmental changes such as temperature, humidity
etc.
SensorManager class
The android.hardware.SensorManager class provides methods :
o to get sensor instance,
o to access and list sensors,
o to register and unregister sensor listeners etc.
You can get the instance of SensorManager by calling the method getSystemService() and
passing the SENSOR_SERVICE constant in it.
SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor class
The android.hardware.Sensor class provides methods to get information of the sensor such
as sensor name, sensor type, sensor resolution, sensor type etc.
SensorEvent class
Its instance is created by the system. It provides information about the sensor.
2. SensorEventListener interface
It provides two call back methods to get information when sensor values (x,y and z) change
or sensor accuracy changes.
Public and abstract methods Description
void onAccuracyChanged(Sensor sensor, int
accuracy)
it is called when sensor accuracy is
changed.
void onSensorChanged(SensorEvent event) it is called when sensor values are
changed.
Methods
getDefaultSensor(int type)
get the default sensor for a given type.
getInclination(float[] I)
computes the geomagnetic inclination angle in radians from the inclination
matrix.
registerListener(SensorListener listener, int sensors, int rate)
registers a listener for the sensor
unregisterListener(SensorEventListener listener, Sensor sensor)
unregisters a listener for the sensors with which it is registered.
getOrientation(float[] R, float[] values)
computes the device's orientation based on the rotation matrix.
getAltitude(float p0, float p)
computes the Altitude in meters from the atmospheric pressure and the pressure
at sea level.
4. view.setBackgroundColor(Color.GREEN);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
lastUpdate = System.currentTimeMillis();
}
//overriding two methods of SensorEventListener
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
}
}
private void getAccelerometer(SensorEvent event) {
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
float accelationSquareRoot = (x * x + y * y + z * z)
/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
long actualTime = System.currentTimeMillis();
Toast.makeText(getApplicationContext(),String.valueOf(accelationSquareRoot)+" "+
SensorManager.GRAVITY_EARTH,Toast.LENGTH_SHORT).show();
if (accelationSquareRoot >= 2) //it will be executed if you shuffle
{
if (actualTime - lastUpdate < 200) {
return;
}
lastUpdate = actualTime;//updating lastUpdate for next shuffle
if (isColor) {
view.setBackgroundColor(Color.GREEN);
} else {
view.setBackgroundColor(Color.RED);
}
isColor = !isColor;
5. }
}
@Override
protected void onResume() {
super.onResume();
// register this class as a listener for the orientation and accelerometer sensors
sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCE
LEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// unregister listener
super.onPause();
sensorManager.unregisterListener(this);
}
}
Example