All Android devices produced worldwide come with built-in sensors that measure motion, orientation, and various environmental conditions. These sensors generally facilitate Android architecture by providing the data from the sensor for various applications. For example, a temperature sensor provides the device’s temperature, information from which could be used for shutting down a few unrequired services. Such a sensor is a general type, but broadly, sensors are divided into three types:
- Motion Sensors: Motion sensors measure the acceleration and rotational forces along the three axes x-y-z. Motion Sensors include accelerometers, gravity sensors, gyroscopes, and rotational vector sensors.
- Environment Sensors: Environment sensors measure a variety of environmental parameters, such as pressure, ambient temperature (room temperature), illumination (light falling on the device), and humidity. They include barometers, photometers, and thermometers.
- Position Sensors: Position Sensors measure the physical position of a device in the space. They include orientation sensors and magnetometers.
Generally Available Sensors in an Android Device
In general, any Android Device on or above Android Version 4.4 (Kitkat) have these sensors present in them:
- Accelerometer – Hardware Sensor – Motion Sensor
- Gravity Sensor – Program Based (Software) – Raw data derived from the Motion Sensors for Gravity calculation.
- Ambient Temperature – Hardware Sensor – Environment Sensor
- Gyroscope – Hardware Sensor – Motion Sensor
- Light Sensor – Hardware Sensor – Environment Sensor
- Orientation Sensor – Program Based (Software) – Raw data derived from the Position and Motion Sensors
- Proximity Sensor – Hardware Sensor – Position Sensor
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Working with the activity_main.xml file
Go to the activity_main.xml file which represents the UI of the application, and create a TextView inside a ScrollView that shall list the sensors present in the device. Below is the code for the activity_main.xml file.
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file, and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Output: Run on Emulator
- How to Find the Screen Resolution of a Device Programmatically in Android?
- How to Vibrate a Device Programmatically in Android?
- How to Display Bluetooth Paired Devices Programmatically in Android?
- How to programmatically hide Android soft keyboard
- Current Internet Connection Type in Real-Time Programmatically in Android
- How to Increase/Decrease Screen Brightness in Steps Programmatically in Android?
- How to Increase/Decrease Screen Brightness using Volume Keys Programmatically in Android?
- How to Detect Touch Event on Screen Programmatically in Android?
- How to Invoke Keyboard Programmatically in Android?
- How to Obtain the Connection Information Programmatically in Android?
- How to Programmatically Enable/Disable Bluetooth in Android?
- How to Change the Screen Orientation Programmatically using a Button in Android?
- How to Programmatically Enable/Disable Wi-Fi in Android?
- How to Adjust the Volume of Android Phone Programmatically from the App?
- How to Control Lottie Animations Programmatically in Android?
- How to Check the Battery Level in Android Programmatically?
- How to Check if the Battery is Charging or Not in Android Programmatically?
- How to Check the Type of Charging (USB/AC) in Android Programmatically?
- How to Maximize/Minimize Screen Brightness Programmatically in Android?
- How to Listen for Volume Button and Back Key Events Programmatically in Android?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.