0% found this document useful (0 votes)
132 views8 pages

Practical 22

This document describes two programs to implement sensors on an Android device. The first program changes the background color when the device is shuffled using the accelerometer sensor. The second program displays the list of all sensors supported by the mobile device.

Uploaded by

Rehan Pathan
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)
132 views8 pages

Practical 22

This document describes two programs to implement sensors on an Android device. The first program changes the background color when the device is shuffled using the accelerometer sensor. The second program displays the list of all sensors supported by the mobile device.

Uploaded by

Rehan Pathan
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/ 8

MAD Experiment 22

Aim: Develop a program to implement sensors.

IX. Exercise:

1. Write a program to changes the background color when device is


shuffled.

MainActivity.java

package com.example.sensor2;
import

androidx.appcompat.app.AppCom

patActivity; import

android.graphics.Color;

import

android.hardware.Sens

or; import

android.hardware.Sens

orEvent;

import

android.hardware.SensorEven

tListener; import

android.hardware.SensorMan

ager; import

android.os.Bundle;

import

android.view.View;

import

android.widget.Tex

tView; import
android.widget.To

ast;

public class MainActivity extends AppCompatActivity implements

SensorEventListener { SensorManager sm;

boolean isColor =

false; TextView

t1;
long timeUpdate;

@Override
public void onCreate(Bundle

savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

t1 =

findViewById(R.id.textVie

w);

t1.setBackgroundColor(Co

lor.YELLOW);

sm = (SensorManager)

getSystemService(SENSOR_SERVICE);

timeUpdate = System.currentTimeMillis();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}

@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() ==

Sensor.TYPE_ACCELEROMETER) { float[]

values = event.values;

float x
=

values[

0];

float y

values[

1];

float z

values[

2];

float accelationspeed = (x * x + y * y + z * z) /

(SensorManager.GRAVITY_EARTH *

SensorManager.GRAVITY_EARTH);

long actualTime =

System.currentTimeMillis(); if

(accelationspeed >= 2)
{
if (actualTime - timeUpdate < 200)
{
return;
}
timeUpdate =

actualTime; if

(isColor) {
t1.setBackgroundColor(Color.GREEN);
}

else

t1.setBackgroundColor(Color.
RED);
}
isColor = !isColor;
} } }
@Override
protected void

onResume() {

super.onResume();

sm.registerListener(this,sm.getDefaultSensor(Sensor.TYPE_ACCELEROM
ETER),

SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void

onPause() {

super.onPause();

sm.unregisterListen

er(this);
}

2. Write a program to display the list of sensors supported by the mobile


device.
MainActivity.java

package com.example.expt22_2;
import

androidx.appcompat.app.AppCompa

tActivity; import

android.content.Context;
import android.hardware.Sensor;
import

android.hardware.Sensor

Manager; import

android.os.Bundle;
import android.widget.TextView;

import java.util.List;
public class MainActivity extends

AppCompatActivity { SensorManager smgr;

TextView

t1;

@Overrid

protected void onCreate(Bundle savedInstanceState)

{ super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

t1=findViewById(R.id.textView);

smgr=(SensorManager)getSystemService(Context.S

ENSOR_SERVICE); List <Sensor>

sensorL=smgr.getSensorList(Sensor.TYPE_ALL);

StringBuilder sb = new StringBuilder();

for(Sensor s:sensorL)
{
sb.append(s.getName()+"\n");
}
t1.setText(sb);

}
}

You might also like