Showing posts with label Sensor. Show all posts
Showing posts with label Sensor. Show all posts

How to Set ProgressBar Progress on Light Sensor Change

API Level 3 or greater needed.


1. Set the Upper Range of a ProgressBar to the Maximum Range of a Light Sensor named myProgressBar.

2. Determine if Sensor Light Changed

3. In the onSensorChanged method, add the following line after the float myEventValues is created. This convert the float value of myEventValues to an integer and set it to the Progress on the myProgressBar.

myProgressBar.setProgress((int)myEventValues);

4. Compile and run!

Resources:

How to Set the Upper Range of a ProgressBar to the Maximum Range of a Light Sensor

For API Level 3 and greater

1. Get the Maximum Range of a Light Sensor and save it so a float named myMaxRange.

2. Set the Maximum Upper Range of a ProgressBar. Instead of setting the max range to 100, we would like to set it to the float named myMaxRange. However, since it is a float, you cannot save a float to an integer. You will need to convert it to an integer first. So, just set the max upper range to (int) myFloat

3. Compile and run!

How to get the Maximum Range of a Light Sensor

public float getMaximumRange () was added in API level 3
Returns: maximum range of the sensor in the sensor's unit.

1. Determine if Android Device has Light Sensor, using SensorManager with a Sensor named mySensor and SensorManager named mySensorManager.

2. In the else of the if/else in step #1,

       a. Declare a Float variable named myMaxRange

       b. Add the below line the float declaration. This will get the maximum range of the light sensor
           and save it to a float variable named myMaxRange

How to Determine if Android Device has Light Sensor, using SensorManager

Needs API Level 3, or greater.
For API Level 7 and above you may use, How to Determine if Android Device has a Light Sensor, using PackageManager.

1. Get the Default Light Sensor with a Sensor named mySensor.

2. Add the below code to the onCreate method. This will test the mySensor to determine if light sensor is available. It will display a toast as determined.

if (mySensor == null){
Toast.makeText(this,  "No Light Sensor Available", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,  "Light Sensor Available", Toast.LENGTH_LONG).show();
}

3. Compile and run!

Resources:

How to Save Sensor TYPE_LIGHT to integer

public static final int TYPE_LIGHT was added in API level 3
A constant describing a light sensor type.

1. Set the Minimum and Target SDK version to 3, or greater. 

2. Declare an integer named intMySensor

3. Add the below line to the onCreate method. This will save the light sensor type(TYPE_LIGHT) to an integer named intMySensor.

intMySensor = Sensor.TYPE_LIGHT;

How to Declare a Sensor object

Sensor was added in API Level 3
Class representing a sensor. Use getSensorList(int) to get the list of available Sensors.

1. Set the Minimum and Target SDK version to 3, or greater. 

2. In the MainActivity.java file, add the below line to the imports section. 

import android.hardware.Sensor;
3. Add the below line to the onCreate method. This will declare a Sensor object named mySensor.

Sensor mySensor; 

4. Compile and run

How to Get the the Context.SENSOR_SERVICE

public static final String SENSOR_SERVICE was added in API level 1
Use with getSystemService(String) to retrieve a SensorManager for accessing sensors.

1. In the MainActivity.java file, add the below line to the import section.

 import android.content.Context;

2. In the MainActivity.java file, add the below line to the onCreate method. This will save the SENSOR_SERVICE to a string named svcName

String svcName = Context.SENSOR_SERVICE;

3. Compile and run!

Next Recommended Article: How to Declare a SensorManager

Resources:
Professional Android 4 Application Development by Reto Meier, pg 482
http://developer.android.com/reference/android/content/Context.html#SENSOR_SERVICE