The android device has sensors we can use in our code. We can get data coming from these resources using the SensorManager object. The following code sample shows how to do it.

package com.abelski.samples;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class MainActivity extends Activity {

	private SensorManager manager;
	private Sensor sensor;
	private SensorEventListener listener = null;

	public void onResume() {
		manager.registerListener(listener, sensor,

	public void onPause() {
		manager.unregisterListener(listener, sensor);

	public void onCreate(Bundle savedInstanceState) {
		manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		sensor = manager.getDefaultSensor(Sensor.TYPE_LIGHT);
		final EditText tf = (EditText) findViewById(;
		listener = new SensorEventListener() {
			public void onAccuracyChanged(Sensor sensor, int accuracy) {
				// ...

			public void onSensorChanged(SensorEvent event) {
				if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
					Log.i("light","Light is "+event.values[0]);
					tf.setText("Light is " + event.values[0]);


The following video clip shows the execution of this code sample and explains it.

Using The Android Sensors PRO

Leave a Reply

Your email address will not be published. Required fields are marked *