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

Practical No 22 (1)

The document contains code for two Android applications that utilize sensors. The first application detects shake events using the accelerometer and changes the background color of the layout, while the second application lists all available sensors on the device. Both applications implement necessary methods for sensor management and UI updates within the Android framework.

Uploaded by

Roshan Kumar
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)
2 views

Practical No 22 (1)

The document contains code for two Android applications that utilize sensors. The first application detects shake events using the accelerometer and changes the background color of the layout, while the second application lists all available sensors on the device. Both applications implement necessary methods for sensor management and UI updates within the Android framework.

Uploaded by

Roshan Kumar
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/ 4

Practical no:-22

Roll No:-121
Name:-Roshan kuma
// Register sensor listener
if (accelerometer != null) { ,
Q1. <?xml version="1.0" encoding="utf-8"?> sensorManager.registerListener(this
<RelativeLayout accelerometer , SensorManager.SENSOR_DELAY_UI)
xmlns:android="http://schemas.android.com/apk/res/ } }
android" @Override
android:id="@+id/main_layout" public void onSensorChanged(SensorEvent event) {
android:layout_width="match_parent" if (event.sensor.getType() ==
android:layout_height="match_parent"> Sensor.TYPE_ACCELEROMETER) {
long currentTime = System.currentTimeMillis();
</RelativeLayout> if ((currentTime - lastUpdate) > 100) {
Main activity .java long diffTime = currentTime - lastUpdate;
astUpdate = currentTime;
package com.example.hel ;
float x = event.values[0];
import android.graphics.Color; float y = event.values[1];
import android.hardware.Sensor; float z = event.values[2];
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener; // Calculate the speed of the shake
import android.hardware.SensorManager; float speed = Math.abs(x + y + z - last_x -
import android.os.Bundle; last_y - last_z) / diffTime * 10000;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.RelativeLayout; if (speed > SHAKE_THRESHOLD) {
// Change the background color when
public class MainActivity extends AppCompatActivity shake is detected
implements SensorEventListener { changeBackgroundColor();
}
private SensorManager sensorManager;
private Sensor accelerometer; last_x = x;
private long lastUpdate = 0; last_y = y;
private float last_x, last_y, last_z; last_z = z;
private static final int SHAKE_THRESHOLD = 800; }
private RelativeLayout mainLayout; }
}
@Override
protected void onCreate(Bundle private void changeBackgroundColor() {
savedInstanceState) { // Random color change logic
super.onCreate(savedInstanceState); int color = Color.rgb((int)(Math.random() * 256),
setContentView(R.layout.activity_main); (int)(Math.random() * 256), (int)(Math.random() *
256));
mainLayout = findViewById(R.id.main_layout); mainLayout.setBackgroundColor(color);
}
// Initialize sensor manager and accelerometer
sensorManager = (SensorManager) @Override
getSystemService(SENSOR_SERVICE); public void onAccuracyChanged(Sensor sensor, int
if (sensorManager != null) { accuracy) {
accelerometer = // Not needed for this example
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELE }
ROMETER);
} @Override
protected void onPause() {
super.onPause();
// Unregister the sensor listener to save battery
sensorManager.unregisterListener(this);
}

@Override
protected void onResume() {
super.onResume();
// Re-register the sensor listener
if (accelerometer != null) {
sensorManager.registerListener(this,
accelerometer, SensorManager.SENSOR_DELAY_UI);
}
}
} output :
Q2. private SensorManager sensorManager;

<?xml version="1.0" encoding="utf-8"?> private ListView sensorListView;


<LinearLayout
private ArrayAdapter<String> sensorAdapter;
xmlns:android="http://schemas.android.com/apk/res/
android" private List<String> sensorList;
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" @Override
android:padding="16dp">
protected void onCreate(Bundle
<TextView savedInstanceState) {
android:id="@+id/sensor_list_title" super.onCreate(savedInstanceState);
android:layout_width="wrap_content"
android:layout_height="wrap_content" setContentView(R.layout.activity_main);
android:text="List of Sensors Supported by Your
Device"
android:textSize="18sp" // Initialize views and variables
android:layout_marginBottom="16dp"/>
sensorListView =
findViewById(R.id.sensor_list_view);
<ListView
android:id="@+id/sensor_list_view" sensorList = new ArrayList<>();
android:layout_width="match_parent"
android:layout_height="match_parent"/> sensorAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, sensorList);
</LinearLayout> sensorListView.setAdapter(sensorAdapter);
Main activity .java

package com.example.sensorslist; // Initialize SensorManager

sensorManager = (SensorManager)
import android.hardware.Sensor; getSystemService(SENSOR_SERVICE);

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener; // Get all sensors available on the device

import android.hardware.SensorManager; if (sensorManager != null) {

List<Sensor> sensors =
import android.os.Bundle;
sensorManager.getSensorList(Sensor.TYPE_ALL);
import android.widget.ArrayAdapter;

import android.widget.ListView;
// Iterate through the list of sensors and add
import androidx.appcompat.app.AppCompatActivity; their names to the list

for (Sensor sensor : sensors) {


import java.util.ArrayList; sensorList.add(sensor.getName());
import java.util.List; }

public class MainActivity extends AppCompatActivity { // Notify the adapter that the data has changed

sensorAdapter.notifyDataSetChanged();
}

} output :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy