Friday 12 June 2015

Measure Light intensity in android | Light sensor in android

No comments
In this android example we learn how to access Light sensor if present in android devise.many mobile having Auto brightness mode function, this function work on light sensor that will adjust screen brightness as per light intensity.

There are many unites such as Lux,candela,lumen etc, to measure light intensity.In this android tutorial we get light intensity in Lux.in this example we reading intensity value and display with progress bar.

Let's go for coding:

CREATE NEW ANDROID PROJECT

Step 1: Write code into activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Androidapplink android example" />

    <TextView
        android:id="@+id/max"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:layout_gravity="center" />

    <ProgressBar
        android:id="@+id/lightmeter"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:padding="20dp"
        android:progress="0" />

    <TextView
        android:id="@+id/reading"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

Step 2: Write code into MainActivity.java

package dev.androidapplink.lightsensorapp;

import android.os.Bundle;
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.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
// declare variable
ProgressBar lightMeter;
TextView tvMaxValue, tvReader;

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

// Load control
lightMeter = (ProgressBar) findViewById(R.id.lightmeter);
tvMaxValue = (TextView) findViewById(R.id.max);
tvReader = (TextView) findViewById(R.id.reading);
// implement sensor manager
SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
// check sensor available in devise. if available then get reading
if (lightSensor == null) {
Toast.makeText(getApplicationContext(), "No Sensor",
Toast.LENGTH_SHORT).show();
// Toast.makeText(AndroidLightSensorActivity.this,
// "No Light Sensor! quit-", Toast.LENGTH_LONG).show();
} else {
float max = lightSensor.getMaximumRange();
lightMeter.setMax((int) max);
tvMaxValue.setText("Max Reading: " + String.valueOf(max));

sensorManager.registerListener(lightSensorEventListener,
lightSensor, SensorManager.SENSOR_DELAY_NORMAL);

}
}

// implement sensor event listener

SensorEventListener lightSensorEventListener = new SensorEventListener() {

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

}

// get sensor update and reading
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
float currentReading = event.values[0];
lightMeter.setProgress((int) currentReading);
tvReader.setText("Current Reading: "
+ String.valueOf(currentReading) + " Lux");
}
}

};

}
NOTE: No need any permission in AndroidManifest.xml

Step 3: Run Your Project:








No comments :

Post a Comment

Follow me Share