0% found this document useful (0 votes)
3 views

SensorApp_Code

The document provides Java code for an Android application that displays a list of sensors available on the device. It initializes a SensorManager, retrieves the sensor list, and shows the sensor names in a Toast message. The accompanying XML layout defines a simple user interface with a TextView for displaying the sensor list.

Uploaded by

Samruddhi Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SensorApp_Code

The document provides Java code for an Android application that displays a list of sensors available on the device. It initializes a SensorManager, retrieves the sensor list, and shows the sensor names in a Toast message. The accompanying XML layout defines a simple user interface with a TextView for displaying the sensor list.

Uploaded by

Samruddhi Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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>

You might also like