Android App to Display List of Sensors
Java Code (MainActivity.java):
package com.example.sensordemo;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
SensorManager sensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the SensorManager
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// Get the list of sensors from the device
java.util.List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
// Show the sensor names in a simple Toast message
StringBuilder sensorNames = new StringBuilder();
for (Sensor sensor : sensors) {
sensorNames.append(sensor.getName()).append("\n");
// Display all sensor names in a Toast message
Toast.makeText(this, sensorNames.toString(), Toast.LENGTH_LONG).show();
XML Layout (activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/sensorTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sensor List will be shown here"
android:textSize="16sp"/>
</LinearLayout>