Android devices have a lot of interesting sensors letting you to make fun applications. Amongst the sensors supported by the Android SDK, the Ambient Temperature sensor lets us to know the temperature measured by an Android device. By using this sensor, we are going to be able to create a Thermometer Application for Android.
Note that you can enjoy this tutorial in video on YouTube :
Create a Thermometer View
Our Thermometer Application will use a specific view to display the current temperature to the user. This specific view will extend the View class from the Android standard SDK.
The Thermometer class will have a setCurrentTemp method letting us to set a new temperature and to redraw the component. We define also a MAX_TEMP and a MIN_TEMP constants to limit the temperatures values displayed by our Thermometer class.
In the init method of the Thermometer class, we get some attributes exposed by the component concerning its color. Then, we define the painters which will be used to draw the Thermometer to the screen.
Finally, in the onDraw method, we start by getting the dimensions of the view to adapt our Thermometer to the screen. Then, we draw the each part of the Thermometer and the graduations.
That gives us the following code :
Previously, we talked about the specific attributes of our Thermometer component. To use these attributes in a layout file, we need to define them in a res/attrs.xml file like that :
Define the User Interface
The User Interface of our Thermometer Application will be really simple with a Thermometer component displayed in the whole screen and the current temperature displayed in text mode in the Action Bar.
So, we define the Thermometer component in the activity_main.xml layout :
Get the current temperature
Now, we can write the Java code of the main activity and trying to get the current temperature provided by the ambient temperature sensor of the device.
To listen the current temperature changes measured by the ambient temperature sensor of the device, we get an instance of the SensorManager object. Then, we call its getDefaultSensor method with the Sensor.TYPE_AMBIENT_TEMPERATURE constant in parameter. With this call, we get an instance of this Sensor. Then, we just have to register this sensor and our activity to be notified from the ambien temperature changes by calling the registerListener of the SensorManager instance got previously.
Our Main Activity implements the SensorEventListener interface, so we are going to get the values in the onSensorChanged method. Once we have the new value for the temperature, we have just to call the setCurrentTemp method of our Thermometer component to update it. Note that we change the title displayed in the Action Bar to display this new temperature in text mode too.
That gives us the following code :
The Thermometer Application in action
Now, we have a complete Thermometer Application for Android. To let you to test this application, I published a more complete version on the Google Play Store :
My Thermometer offers you to get temperature with 5 different sources :
– Ambient Temperature Sensor if your device features this kind of sensor
– Extrapolated Ambient Temperature calculated from the battery temperature of your device
– Battery Temperature
– CPU Temperature
– Weather Temperature that gives you temperature from the closest Weather Station and uses your GPS Location
My Thermometer supports temperature’s display in degrees Celsius and in degrees Farenheit. Don’t hesitate to give me your feedback and ideas to improve My Thermometer in comments.
Simulate the temperature changes
Unfortunately, just some devices have an Ambient Temperature sensor inside like the Samsung Galaxy S4 for example. So, for a lot of developers, with this code, you gave a good change to have the following message on your screen :
No Ambient Temperature Sensor !
To have a working User Interface even without an Ambient Temperature, we are going to use a little hack to simulate the temperature changes in our application.
We create a simulateAmbientTemperature method with a Timer used to schedule a specific task at fixed rate. Each 3 seconds, it will execute a TimerTask. In this task, we will define a random temperature value which we will apply on the Thermometer component.
With this little hack, the code of our MainActivity will be like that :
That’s all for that tutorial. To discover more tutorials on Android Development, don’t hesitate to visit the SSaurel’s Blog : https://www.ssaurel.com/blog