Friday 25 October 2013

In Room With No Light

Android Sensor(Light Sensor)

In Room With Dim Light

Android Sensor(Light Sensor)

In Room With Normal Light

Android Sensor(Light Sensor)

Create new Android Project
Project Name: LightSensor
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: LightSensor
Package Name: com.shaikhhamadali.blogspot.lightsensor
Create layout file: activity_light_sensor

1.create layout:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".LightSensor" >
 <TextView
        android:id="@+id/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Light Sensor"
        android:textColor="#0835C9"
        android:textSize="30sp" />
    <TextView 
    android:id="@+id/tVMaxValue"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/TextView"
    android:textSize="20sp"/>
 <TextView 
    android:id="@+id/tVCurrentLight"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/tVMaxValue"
    android:layout_marginTop="10dp"
    android:textSize="20sp"
    />

</RelativeLayout>

2.code of activity:


package com.shaikhhamadali.blogspot.lightsensor;

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.widget.TextView;
import android.widget.Toast;
 
public class LightSensor extends Activity implements SensorEventListener{
 //SensorManager lets you access the device's sensors
 //declare Variables
 private SensorManager sensorManager;
    TextView tVMaxValue, tVCurrentLight;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_light_sensor);
        tVMaxValue = (TextView)findViewById(R.id.tVMaxValue);
        tVCurrentLight = (TextView)findViewById(R.id.tVCurrentLight);
  //create instance of sensor manager and get system service to interact with Sensor
        sensorManager= (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        Sensor lightSensor= sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
      
        if (lightSensor == null){
         Toast.makeText(LightSensor.this,"No Light Sensor Found! ",Toast.LENGTH_LONG).show();
        }else{ float max =  lightSensor.getMaximumRange();
          //Get Maximum Value From Light sensor
           tVMaxValue.setText("Max Range: " + String.valueOf(max));
           sensorManager.registerListener(this,lightSensor,SensorManager.SENSOR_DELAY_NORMAL);
        }
    }
     
    @Override
 protected void onResume() {
  super.onResume();
  // register this class as a listener for the lightSensor
  sensorManager.registerListener(this,
    sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
    SensorManager.SENSOR_DELAY_NORMAL);
 }
    @Override
 protected void onPause() {
  // unregister listener
  super.onPause();
  sensorManager.unregisterListener(this);
 }
 @Override
 public void onAccuracyChanged(Sensor sensor, int accuracy) {
  
 }
 // called when sensor value have changed
 @Override
 public void onSensorChanged(SensorEvent event) {
  // The light sensor returns a single value.
     // Many sensors return 3 values, one for each axis.
     if(event.sensor.getType()==Sensor.TYPE_LIGHT){
       float currentLight = event.values[0];
       if(currentLight<1){tVCurrentLight.setText("Current light: " + "no Light");}
       else if(currentLight<5){tVCurrentLight.setText("Current light is Dim:" + String.valueOf(currentLight));}
       else if(currentLight<10){tVCurrentLight.setText("Current light is Normal:" + String.valueOf(currentLight));}
       else if(currentLight<100){tVCurrentLight.setText("Current light is Bright(Room):" + String.valueOf(currentLight));}
       else tVCurrentLight.setText("Current light is Bright(Sun):" + String.valueOf(currentLight));
 }}
}

3. note that:


4. conclusion:


  • Some information about how to use TYPE_LIGHT sensor.
  • know what are Sensors and how to use Sensors.
  • know that light readings change in different light.
5. About the post:

  • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
  •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  •  Hope you enjoy it!

6. Source Code:

        you can download the source code here