Practicalno:-22 1. Write A Program To Changes The Background Color When Device Is Shuffled. Acitivity - Main - XML
Practicalno:-22 1. Write A Program To Changes The Background Color When Device Is Shuffled. Acitivity - Main - XML
float y = values[1];
float z = values[2]; @Override
float accelationSquareRoot = (x * x + y protected void onPause() {
* y + z * z) / super.onPause();
(SensorManager.GRAVITY_EARTH * sensorManager.unregisterListener(this);
SensorManager.GRAVITY_EARTH); }
long actualTime = }
System.currentTimeMillis();
Toast.makeText(getApplicationContext(
), String.valueOf(accelationSquareRoot) ❖ Output
+""+
SensorManager.GRAVITY_EARTH,
Toast.LENGTH_SHORT).show();
if (accelationSquareRoot >= 2) //it
will be executed if you shuffle
{
if (actualTime - lastUpdate < 200) {
return;
}
lastUpdate = actualTime;
lastUpdate for next shuffle
if (isColor) {
view.setBackgroundColor(Color.GREE
N);
} else {
view.setBackgroundColor(Color.RED);
}
isColor = !isColor;
}
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor
.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NO
RMAL);
}
Page 1
[Type the document title]
❖ Activiy_main.xml
Page 2
[Type the document title]
❖ Output
Page 3