1. Code
  2. Android SDK

Building Apps with Environment Sensors

Read Time:11 minsLanguages:

Learn how to use the Android Environment Sensors to detect information about the user's environment, including ambient temperature, pressure, humidity, and light.

The Android system supports a range of device Sensors, some implemented in hardware and some in software. The Environment Sensors are all hardware features, providing access to information about ambient temperature, pressure, humidity, and light. These sensors return values as follows: temperature is measured in degrees Celsius, atmospheric pressure in hPa millibars, relative ambient air humidity as a percentage value, and ambient light in SI lux units. In this tutorial, we will run through the basic process for using these four main Environment Sensors. We will not be using the device temperature sensor, as it is now deprecated as of Android 4.0.

There are many possible applications for these sensors, such as barometers and thermometers. You may have come across such apps in Google Play already, but it is worth noting that they may not necessarily be implementing their functions using Environment Sensors. For example, weather apps often use location data fetched over the Web to determine environment information based on where you are.

Since these sensors are provided via users' hardware, support does vary between devices and manufacturers. At the time of writing, very few Android smartphones or tablets support all of the Environment Sensors, but many of the more recent models support one or more of them. It is vital to carry out checks on whether the user has particular sensors and to avoid using functionality that is totally reliant on them. The only exception to this is if you ensure only users with the required hardware can download your application - you can do this using the filters for an app as listed in the Google Play store.

Step 1: Create a New Android Project

Create a new Android project in Eclipse and give it a name of your choice. Let Eclipse create a main Activity, as this is the only class we will need. For the code used in this tutorial, we are targeting Android API level 14 (Android 4.0 - Ice Cream Sandwich), but you can target a more recent version if you wish. You do not need to make any alterations to the Manifest file. Your main Activity class should have the following initial structure, with your chosen class name:

We are going to implement a couple of Interfaces, so extend your opening class declaration line as follows:

The click listener is for user interaction and the Sensor Event Listener is for receiving data from the device sensors. Eclipse should have provided import statements for the Activity and Bundle classes, but you also need to add the following to the list:

We will be adding code to the class later.

Step 2: Design User Interaction

In order to demonstrate the basic process for using the Environment Sensors, we are going to build a simple user interface. The app will display a list of four buttons, one for each of the sensors we will be using. When the user selects a button, the app will attempt to retrieve the appropriate information and present it within a Text View. First, let's define some text Strings we will use within the interface. Open your "res/values/strings.xml" file and edit it to contain the following:

These represent the title, introductory text, button labels, and a placeholder for the Text Views. We are going to use a couple of drawable resources for the design, but you can omit them if you wish. To use them, in each of your app's drawables folders, you will need to create two additional files, "back.xml" and "btn.xml" (select each drawable folder in turn and choose "File" > "New" > "File", then enter the file name). For the "back.xml" file, enter the following code:

For the "btn.xml" file, enter the following:

The back drawable is for the Activity background and the "btn" drawable is for the button backgrounds. Feel free to alter these designs in any way you wish - make sure you copy them to each drawable folder in your app.

Now open your app's "main.xml" layout file (res/layout/main.xml). Enter a Scroll View and Linear Layout as follows:

Inside the Linear Layout, add the introduction, then a Button and Text View for each of the four sensors:

Each Button and Text View pair is virtually identical, with ID attributes to identify them in the Java code. Of course, you can alter any of the design elements if you wish. The layout refers to the drawable resources and Strings.

Step 3: Handle User Interaction

Open your app's main Activity class. At the top of the class declaration, before the "onCreate" method, add the following instance variables:

These represent the Buttons and Text Views we created in the layout. We are going to use an array to keep track of the Text View items, as these will update with information when the sensors return it, so add the following array variable next:

Now add these constants to refer to each sensor type:

Inside the "onCreate" method, after the line in which the content view is set, retrieve a reference to each button using the ID attributes we included in the layout, as follows:

Now set each of these to use the Activity class as click listener:

When these buttons are pressed, the Activity "onClick" method will execute. Next, retrieve the Text View items and add a reference to each in the array we declared, using the constants to specify each index:

Now we need to provide the "onClick" method, adding it to the class after the "onCreate" method:

Inside the method, we need to determine which button was clicked using a conditional:

Inside each of these we will attempt to retrieve the relevant environment data.

Step 4: Setup Environment Sensor

At the top of the class, add a couple of variables for the environment sensing process:

Back in the "onCreate" method, after the existing code, add the following to create an instance of the Sensor Manager class:

We need the Sensor Manager for all environment sensing processes. We use it to retrieve specific sensors. Inside the "onClick" method "if" statement for ambient temperature, attempt to retrieve the ambient temperature sensor as follows:

Now we need to take care of cases in which the sensor is not provided by the user device, so add the following test:

We simply output an error message. If the sensor is present, we need to register to receive the data it returns, so add the following after this "if" statement:

Now carry out the same process for each of the buttons in the "onClick" method. For the light button (in its "if" statement inside "onClick"):

Notice that here we are requesting the "TYPE_LIGHT" sensor and are tailoring the error message to the sensor type. For the pressure button:

Finally, in the "if" statement for the humidity button:

Step 5: Retrieve Accuracy Data

In addition to returning the requested environment data, the sensor will also return accuracy data. Add the following method to your class, which is required when implementing the Sensor Event Listener Interface:

Inside the method, start building a message regarding the accuracy:

We will use a switch statement on the passed accuracy integer parameter:

We tailor the message to the sensor's accuracy level, using the Sensor Manager class. Output the accuracy when it is received as follows, but after the switch statement:

Inside the "onAccuracyChanged" method you can also determine the sensor type from the passed parameter if you need to.

Step 6: Retrieve Sensor Data

Now we can finally retrieve the returned data from the sensors using a Sensor Event - we do this in the "onSensorChanged" method, which is also required in order to implement the Interface:

The Sensor Event returns its data in different ways depending on the sensor type. For all four types we are using, it is retrieved in the same way, from the first item in an array of floating point values. Add the following inside the method:

We are now going to build a text String including this data, and write it to the relevant Text View for the user to see. First, we create a Text View variable and give it a default value (this value will be overwritten - we include it to keep Eclipse happy):

Next, we declare the String:

The content of the String is going to depend on the type of sensor, so let's find out which one it is:

Now we can use a switch statement on this value:

In each case, we build the informative String using the sensor value retrieved and a text excerpt relevant to the type. We also set the Text View to the relevant user interface item using the array and constants. After the switch statement, output the information:

Now reset the sensor variable and stop listening for updates to prevent unnecessary battery usage:

Finally, we don't want the app to use unnecessary resources when it is paused, so add this method to the class:

Step 7: Try it Out

That's the complete demonstration app! There is no point running this app on the default Android emulator, because it does not provide Environment Sensors. However, you can either run it on an actual device or use the Sensor Simulator tool, which allows you to simulate certain aspects of the environment. The following is a screenshot of the app running on the Samsung Galaxy S III just after retrieving the light and pressure data:

Here it is for the other two sensors, which are not supported:


The Environment Sensors are an exciting but still developing feature of the Android platform. However, it is a little early to focus on them. If you want to explore using these sensors in more advanced ways, check out the Developer Guide section on calculating dew point and absolute humidity levels based on relative humidity and ambient temperature. Other than that - try to be patient!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.