0% found this document useful (0 votes)
9 views2 pages

Prac 22

The document outlines a program implementation for sensors in Android, specifically using an accelerometer. It includes XML layout for the main activity and Java code for handling sensor events. The program registers and unregisters the sensor listener based on the activity lifecycle methods.

Uploaded by

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

Prac 22

The document outlines a program implementation for sensors in Android, specifically using an accelerometer. It includes XML layout for the main activity and Java code for handling sensor events. The program registers and unregisters the sensor listener based on the activity lifecycle methods.

Uploaded by

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

Practical No:=22

Title:= Develop a program to implement Sensors


Rollno:=26 Bath:=B DOP:=11/03/25
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> super.onCreate(savedInstanceState);
<LinearLayout
xmlns:android="http://schemas.android.com setContentView(R.layout.activity_main);
/apk/res/android"
android:layout_width="match_parent" sensorData =
android:layout_height="match_parent" findViewById(R.id.sensorData);
android:orientation="vertical"
android:gravity="center" sensorManager = (SensorManager)
android:padding="20dp"> getSystemService(SENSOR_SERVICE);

<TextView if (sensorManager != null) {


android:id="@+id/sensorData" accelerometer =
android:layout_width="wrap_content" sensorManager.getDefaultSensor(Sensor.TY
android:layout_height="wrap_content" PE_ACCELEROMETER);
android:text="sensor data" }
android:textSize="25sp" }
android:textStyle="bold"/> @Override
</LinearLayout> public void
onSensorChanged(SensorEvent event) {
MainActivity.java if (event.sensor.getType() ==
package com.example.sensors_demo; Sensor.TYPE_ACCELEROMETER)
import android.hardware.Sensor; {
import android.hardware.SensorEvent; float x = event.values[0];
import String sensorText = "X: " + x;
android.hardware.SensorEventListener; sensorData.setText(sensorText);
import android.hardware.SensorManager; }
import android.os.Bundle; }
import android.widget.TextView; @Override
import public void onAccuracyChanged(Sensor
androidx.appcompat.app.AppCompatActivit sensor, int accuracy) {
y; }
@Override
public class MainActivity extends protected void onPause() {
AppCompatActivity implements super.onPause();
SensorEventListener { if (sensorManager != null)
SensorManager sensorManager; {
Sensor accelerometer;
TextView sensorData; sensorManager.unregisterListener(this);
}
@Override }
protected void onCreate(Bundle @Override
savedInstanceState) { protected void onResume() {
super.onResume(); SensorManager.SENSOR_DELAY_NORMA
if (sensorManager != null && L);
accelerometer != null) { }
sensorManager.registerListener(this, }
accelerometer, }
OUTPUT:=

You might also like