May 11, 2011

How to use the Light sensor on Android

If your android device has a Light Sensor you can use it in your applications. It provide the value in lux units as described here. More about lux find here.


import java.util.Date;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class main extends Activity implements SensorEventListener {
 private  SensorManager sensorManager = null;
 private  Sensor currentSensor = null; 
 public void onResume(){
  if(currentSensor != null)sensorManager.registerListener(this, currentSensor, SensorManager.SENSOR_DELAY_FASTEST);
 public void onPause(){
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
     sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
  currentSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT );
  if(currentSensor != null){
   sensorManager.registerListener(this, currentSensor, SensorManager.SENSOR_DELAY_FASTEST);
   ((TextView) findViewById("Can't initialize the LIGHT sensor.");   

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

 public void onSensorChanged(SensorEvent event) {
  // TODO Auto-generated method stub
  if (event.sensor.getType() == Sensor.TYPE_LIGHT){   
   TextView tv = (TextView) findViewById(;
   tv.setText( tv.getText()+ "value: " +event.values[0] + " lux , time: " + new Date() + "\n");   

8 comentarii:

deep vats said...

nice , it is the basic step of this
nice blog and information ,
android application development please keep it up

Anonymous said...


What need I put into the manifest file?

Is this will be correct?

is permission needed?

Anonymous said...

lost this line:
uses-feature android:name="android.hardware.sensor.light"

John Smith said...

Android Application Developers : Don't select a developer who has just proved helpful on a specific app development, for e.g. you shouldn't select somebody who only has experience of creating applications for the training and learning category. The reason being that just operating on an app for a particular category is not going to help designers gain the kind of skills needed to build exclusive and unique applications for other groups, that they haven't previously worked on.

From.Ae said...

Well, I have got the best information from here the site is fully stuffed with the knowledgeable information.

Android App Developer in Pakistan

Alex said...

Been reading this site for awhile now, always has really good posts and topics please keep it up! loads of blogs are going under lately from lack of new posts etc
android apps developer

Elite Smartcare said...

I read your blog such a wonderful details you added in this post I really like it. Thanks for sharing information. Mobile Application Development

Yasmin J said...

Nice info you posted here! Keep up the excellent work!
Mobile app developers India

Post a Comment