Skip to content

Commit aab95e8

Browse files
committed
Updated the Sensor Tutorial
1 parent 596d561 commit aab95e8

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

‎de.vogella.android.sensor/res/layout/main.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
android:orientation="vertical" android:layout_width="fill_parent"
44
android:layout_height="fill_parent">
55
<TextView android:id="@+id/textView" android:layout_width="match_parent"
6-
android:layout_height="match_parent" android:text="Shake to get a toast" />
6+
android:layout_height="match_parent" android:text="Shake to get a toast and to switch color" />
77
</LinearLayout>

‎de.vogella.android.sensor/src/de/vogella/android/sensor/SensorTest.java‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414

1515
public class SensorTest extends Activity implements SensorEventListener {
1616
private SensorManager sensorManager;
17-
private float lastX = 0;
18-
private float lastY = 0;
19-
private float lastZ = 0;
17+
private boolean color = false;
2018
private View view;
19+
private long lastUpdate;
2120

2221
/** Called when the activity is first created. */
2322
@Override
@@ -35,6 +34,7 @@ public void onCreate(Bundle savedInstanceState) {
3534
sensorManager.registerListener(this,
3635
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
3736
SensorManager.SENSOR_DELAY_NORMAL);
37+
lastUpdate = System.currentTimeMillis();
3838
}
3939

4040
@Override
@@ -48,11 +48,22 @@ public void onSensorChanged(SensorEvent event) {
4848

4949
float accelationSquareRoot = (x * x + y * y + z * z)
5050
/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
51+
long actualTime = System.currentTimeMillis();
5152
if (accelationSquareRoot >= 2) //
5253
{
54+
if (actualTime - lastUpdate < 200) {
55+
return;
56+
}
57+
lastUpdate = actualTime;
5358
Toast.makeText(this, "Device was shuffed", Toast.LENGTH_SHORT)
5459
.show();
55-
view.setBackgroundColor(Color.RED);
60+
if (color) {
61+
view.setBackgroundColor(Color.GREEN);
62+
63+
} else {
64+
view.setBackgroundColor(Color.RED);
65+
}
66+
color = !color;
5667
}
5768

5869
}

0 commit comments

Comments
 (0)