0% found this document useful (0 votes)
14 views3 pages

Sensors

The document contains a Java class named SensorColor that extends AppCompatActivity and implements SensorEventListener to detect accelerometer sensor changes. It changes the background color of a layout when a shake is detected, based on a defined threshold. The class includes methods for lifecycle management and sensor event handling, utilizing random color generation for the background change.

Uploaded by

arnav.sen2006
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)
14 views3 pages

Sensors

The document contains a Java class named SensorColor that extends AppCompatActivity and implements SensorEventListener to detect accelerometer sensor changes. It changes the background color of a layout when a shake is detected, based on a defined threshold. The class includes methods for lifecycle management and sensor event handling, utilizing random color generation for the background change.

Uploaded by

arnav.sen2006
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

Sensors

SensirColor.java

public class SensorColor extends AppCompatActivity implements SensorEventListener {


private SensorManager sensorManager;
private Sensor accelerometer;
private long lastUpdate = 0; // time in millisecond
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 600; // speed value

private LinearLayout myLayout;


private Random random;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor_color);

myLayout = findViewById(R.id.main); // Ensure you have a RelativeLayout in your layout


file
random = new Random();

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);


if (sensorManager != null) {
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
}

@Override
protected void onResume() {
super.onResume();
if (accelerometer != null) {
sensorManager.registerListener((SensorEventListener) this, accelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener((SensorListener) this);
}

@Override
public void onSensorChanged(SensorEvent event)
{
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];

long currentTime = System.currentTimeMillis();


if ((currentTime - lastUpdate) > 100) { // Adjust the interval
long di Time = currentTime - lastUpdate;
lastUpdate = currentTime;

float speed = Math.abs(x + y + z - last_x - last_y - last_z) / di Time * 10000;

if (speed > SHAKE_THRESHOLD) {


changeBackgroundColor();
}
last_x = x;
last_y = y;
last_z = z;
}
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Not needed for this example
}
private void changeBackgroundColor() {
int color = Color.rgb( random.nextInt(256), random.nextInt(256), random.nextInt(256));
myLayout.setBackgroundColor(color);
}

You might also like