Download Code

In this tutorial you will learn about Sensor and SensorManager in android.Let me give you some basic information about sensor in android.

Sensor

Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. These sensor are capable of providing raw data with high precision and accuracy.There are three main categories of Sensors.

Hardware and Software based Sensors.

SensorManager

SensorManager allows to access the device’s sensors. Get an instance of this class by calling Context.getSystemService() with the argument SENSOR_SERVICE.

Alright let’s start create new android application project.Basically you will implement ACCELEROMETER which will give you the X,Y co-ordinates according to your device movement.

Don’t touch activity_main.xml because you don’t need it.

sensor,sensormanager,sensors android tutorial,sensors in android tutorial,how to use sensors in android

Go to your MainActivity.java class by default it will be.

package developer.codecsensors;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Context;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends Activity{

	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
    }

}

Next implement an interface called SensorEventListener and add unimplemented methods.

MainActivity.java

package developer.codecsensors;

import android.support.v7.app.ActionBarActivity;
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.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity implements SensorEventListener{	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        
    }

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}  
}

Next you will get the SensorManager variable to access your device Sensors.After that register SensorListener.Get the values of movement in OnSensorChanged() method.Complete class.
MainActivity.java

package developer.codecsensors;

import android.support.v7.app.ActionBarActivity;
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.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends Activity implements SensorEventListener{

	SensorManager sm;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0){
        	Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        	sm.registerListener(this, s	, SensorManager.SENSOR_DELAY_NORMAL);
        	
        }
        
    }

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		Toast.makeText(getApplicationContext(), String.valueOf(event.values[0])+ " , " + String.valueOf(event.values[1]), Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

   
}

Now your android application project.When app is on it will show you X,Y co-ordinates in Toast.

sensor,sensormanager,sensors android tutorial,sensors in android tutorial,how to use sensors in android

that’s it for this tutorial.

Thanks.